Results 1 to 6 of 6

Thread: Netload Example

  1. #1
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,106
    Login to Give a bone
    0

    Default Netload Example

    This was compiled with AutoCAD2010 and would need to be re-compiled for 2013 and 2014 (or 2009 and before)

    It also makes a good template.


    Code:
    '
    ' Permission to use, copy, modify, and distribute this software in
    ' object code form for any purpose and without fee is hereby granted, 
    ' provided that the above copyright notice appears in all copies and 
    ' that both that copyright notice and the limited warranty and
    ' restricted rights notice below appear in all supporting 
    ' documentation.
    '
    ' WE PROVIDE THIS PROGRAM "AS IS" AND WITH ALL FAULTS. 
    ' WE SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
    ' MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. 
    ' DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
    ' UNINTERRUPTED OR ERROR FREE.
    '
    ' Use, duplication, or disclosure by the U.S. Government is subject to 
    ' restrictions set forth in FAR 52.227-19 (Commercial Computer
    ' Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
    ' (Rights in Technical Data and Computer Software), as applicable.
    '
    Imports Autodesk.AutoCAD.ApplicationServices
    Imports Autodesk.AutoCAD.DatabaseServices
    Imports Autodesk.AutoCAD.EditorInput
    Imports Autodesk.AutoCAD.Runtime
    'Imports objACADApplication = Autodesk.AutoCAD.ApplicationServices.Application
    
    Imports System.Reflection
    
    
    Public Class NetloadClass
    
    
        ''' <summary>
        '''  Lisp function for loading a .net dll
        ''' LISP Syntax: (netload "mynetassembly.dll")
        ''' </summary>
    
        <LispFunction("Netload")> _
        Public Function Netload(ByVal rbfNetAssemblyFile As ResultBuffer)
            Try
                Dim arrNetAssemblyFile As TypedValue() = rbfNetAssemblyFile.AsArray()
                If arrNetAssemblyFile.Length > 0 Then
                    Dim strFileName As String = arrNetAssemblyFile(0).Value.ToString
                    If Netload(strFileName) Then
                        Return New TypedValue(LispDataType.T_atom, -1)
                    End If
                End If
            Catch Ex As System.Exception
                WriteMessage("; error Loading Assembly: " & Ex.Message & vbLf)
            End Try
            Return New TypedValue(LispDataType.Nil, -1)
        End Function
    
    
        ''' <summary>
        '''  Function to load a net assembly (over load of lisp version for .net)
        ''' </summary>
    
        Public Function Netload(ByVal strFileName As String) As Boolean
            Dim docThisDrawing As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Try
                Dim strFullName As String = _
                         HostApplicationServices.Current.FindFile(strFileName, docThisDrawing.Database, FindFileHint.Default)
                WriteMessage(strFullName & vbLf)
                System.Reflection.Assembly.LoadFrom(strFullName)
                Return True
            Catch Ex As System.Exception
                WriteMessage("Error Loading Assembly: " & Ex.Message & vbLf)
            End Try
            Return False
        End Function
    
    
        ''' <summary>
        ''' Simple print to command window function
        ''' </summary>
    
        Public Function WriteMessage(ByVal strPrintString) As Boolean
            On Error GoTo OnError
            Dim docThisDrawingP As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            docThisDrawingP.Editor.WriteMessage("" & strPrintString & vbLf & "")
            Return True
    OnError: Return False
        End Function
    
    End Class
    Attached Files Attached Files
    Last edited by peter; 2014-01-04 at 08:45 AM.
    AutomateCAD

  2. #2
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,106
    Login to Give a bone
    0

    Default Re: Netload Example

    Setting up configuration for VB.NET

    These screen shots are from VS2008 but are similar to other VS products.

    If you highlight My Project in the Solution Explorer.



    In the middle tile you will see the application tab highlight first.

    In this case I called the assembly (the file you netload into AutoCAD) NetloadAssembly(.dll) and the root namespace in
    this case I called NetloadNameSpace (although I didn't use a namespace in the Class1 class of this assembly.

    c\

    Inside the folder structure for this file I indicate the build location as Bin\Release but when debugging the assembly will
    be found under bin\debug\

    Option explicit requires for the code to define all of the variables.
    The rest are all default values.

    Generate XML documentation file reads the comments between the ''' lines in the program.



    For AutoCAD functions, in order to test them you need to run AutoCAD. Below I have it run acad.exe for 2010.
    I also can specify where the file will run in the working directory.



    The references are very important!!! the acdbmgd, acmgd, (and in 2013 and later acdbcoremgd) libraries are required.
    Also I have included below the AutoCAD.Interop and AutoCAD.Interop.Common libraries that are available to download in the
    ObjectARX SDK (in both 32 and 64 bit flavors). I put them in the same directory as the acdbmgd libraries.

    I have not capitalized on them in this assembly but I put them there just so I can show them to you now.

    The system. libraries are mircosoftism's and should also be included.





    After selecting the add button. You can select the browse tab to find the acdbmgd.dll etc... files and add them to your reference.
    Last edited by peter; 2014-01-05 at 02:20 AM.

  3. #3
    Certifiable AUGI Addict
    Join Date
    2015-11
    Location
    Jo'burg SA
    Posts
    4,512
    Login to Give a bone
    0

    Default Re: Netload Example

    As requested, here's the C# of this code - converted using SharpDevelop's one-click project converter (full cs project attached):
    Code:
    using Microsoft.VisualBasic;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    // (C) Copyright 2014 by Peter Jamtgaard
    //
    // Permission to use, copy, modify, and distribute this software in
    // object code form for any purpose and without fee is hereby granted, 
    // provided that the above copyright notice appears in all copies and 
    // that both that copyright notice and the limited warranty and
    // restricted rights notice below appear in all supporting 
    // documentation.
    //
    // AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS. 
    // AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
    // MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK, INC. 
    // DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
    // UNINTERRUPTED OR ERROR FREE.
    //
    // Use, duplication, or disclosure by the U.S. Government is subject to 
    // restrictions set forth in FAR 52.227-19 (Commercial Computer
    // Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
    // (Rights in Technical Data and Computer Software), as applicable.
    //
    using Autodesk.AutoCAD.ApplicationServices;
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.EditorInput;
    using Autodesk.AutoCAD.Runtime;
    //Imports objACADApplication = Autodesk.AutoCAD.ApplicationServices.Application
    
    using System.Reflection;
    namespace NetLoad
    {
    
    
    	public class NetloadClass
    	{
    		/// <summary>
    		///  Lisp function for loading a .net dll
    		/// LISP Syntax: (netload "mynetassembly.dll")
    		/// </summary>
    
    		[LispFunction("Netload")]
    		public object Netload(ResultBuffer rbfNetAssemblyFile)
    		{
    			try {
    				TypedValue[] arrNetAssemblyFile = rbfNetAssemblyFile.AsArray();
    				if (arrNetAssemblyFile.Length > 0) {
    					string strFileName = arrNetAssemblyFile[0].Value.ToString();
    					if (Netload(strFileName)) {
    						return new TypedValue(LispDataType.T_atom, -1);
    					}
    				}
    			} catch (System.Exception Ex) {
    				WriteMessage("; error Loading Assembly: " + Ex.Message + Constants.vbLf);
    			}
    			return new TypedValue(LispDataType.Nil, -1);
    		}
    
    		/// <summary>
    		///  Function to load a net assembly (over load of lisp version for .net)
    		/// </summary>
    		public bool Netload(string strFileName)
    		{
    			Document docThisDrawing = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
    			try {
    				string strFullName = HostApplicationServices.Current.FindFile(strFileName, docThisDrawing.Database, FindFileHint.Default);
    				WriteMessage(strFullName + Constants.vbLf);
    				System.Reflection.Assembly.LoadFrom(strFullName);
    				return true;
    			} catch (System.Exception Ex) {
    				WriteMessage("Error Loading Assembly: " + Ex.Message + Constants.vbLf);
    			}
    			return false;
    		}
    		/// <summary>
    		/// Simple print to command window function
    		/// </summary>
    		public bool WriteMessage(strPrintString)
    		{
    			 // ERROR: Not supported in C#: OnErrorStatement
    
    			Document docThisDrawingP = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
    			docThisDrawingP.Editor.WriteMessage("" + strPrintString + Constants.vbLf + "");
    			return true;
    			OnError:
    			return false;
    		}
    
    	}
    }
    Note the Conversion Results showed an error:
    Code:
    1 error(s) converting C:\####\NetLoad\NetLoad\Class.vb:
    -- line 72 col 9: Not supported in C#: OnErrorStatement
    That would need some manual modification as C# does not allow for the VB "On Error Goto" statement. In C# you'd rather use a try-catch-finally block.
    Attached Files Attached Files

  4. #4
    Certifiable AUGI Addict
    Join Date
    2015-11
    Location
    Jo'burg SA
    Posts
    4,512
    Login to Give a bone
    0

    Default Re: Netload Example

    And then just for fun ... here's the VB code converted to Ruby. No errors noted as per the C# conversion, though from the code I'm a bit concerned that it didn't even note a warning
    Code:
    require "mscorlib"
    require "Autodesk.AutoCAD.ApplicationServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    require "Autodesk.AutoCAD.DatabaseServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    require "Autodesk.AutoCAD.EditorInput, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    require "Autodesk.AutoCAD.Runtime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" #Imports objACADApplication = Autodesk.AutoCAD.ApplicationServices.Application
    require "System.Reflection, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    
    class NetloadClass
    	def Netload(rbfNetAssemblyFile) # <summary> # Lisp function for loading a .net dll # LISP Syntax: (netload "mynetassembly.dll") # </summary>
    		begin
    			arrNetAssemblyFile = rbfNetAssemblyFile.AsArray()
    			if arrNetAssemblyFile.Length > 0 then
    				strFileName = self.arrNetAssemblyFile(0).Value.ToString
    				if self.Netload(strFileName) then
    					return TypedValue.new(LispDataType.T_atom, -1)
    				end
    			end
    		rescue System.Exception => Ex
    			self.WriteMessage("; error Loading Assembly: " + Ex.Message + vbLf)
    		ensure
    		end
    		return TypedValue.new(LispDataType.Nil, -1)
    	end
     # <summary> # Function to load a net assembly (over load of lisp version for .net) # </summary>
    	def Netload(strFileName)
    		docThisDrawing = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
    		begin
    			strFullName = HostApplicationServices.Current.FindFile(strFileName, docThisDrawing.Database, FindFileHint.Default)
    			self.WriteMessage(strFullName + vbLf)
    			System.Reflection.Assembly.LoadFrom(strFullName)
    			return true
    		rescue System.Exception => Ex
    			self.WriteMessage("Error Loading Assembly: " + Ex.Message + vbLf)
    		ensure
    		end
    		return false
    	end
     # <summary> # Simple print to command window function # </summary>
    	def WriteMessage(strPrintString)
    		docThisDrawingP = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
    		docThisDrawingP.Editor.WriteMessage("" + strPrintString + vbLf + "")
    		return true
    		return false
    	end
    end
    And then the Python version:
    Code:
    from Autodesk.AutoCAD.ApplicationServices import *
    from Autodesk.AutoCAD.DatabaseServices import *
    from Autodesk.AutoCAD.EditorInput import *
    from Autodesk.AutoCAD.Runtime import * #Imports objACADApplication = Autodesk.AutoCAD.ApplicationServices.Application
    from System.Reflection import *
    
    class NetloadClass(object):
    	def Netload(self, rbfNetAssemblyFile): # <summary> #  Lisp function for loading a .net dll # LISP Syntax: (netload "mynetassembly.dll") # </summary>
    		try:
    			arrNetAssemblyFile = rbfNetAssemblyFile.AsArray()
    			if arrNetAssemblyFile.Length > 0:
    				strFileName = self.arrNetAssemblyFile(0).Value.ToString
    				if self.Netload(strFileName):
    					return TypedValue(LispDataType.T_atom, -1)
    		except System.Exception, Ex:
    			self.WriteMessage("; error Loading Assembly: " + Ex.Message + vbLf)
    		finally:
    		return TypedValue(LispDataType.Nil, -1)
    
    	def Netload(self, strFileName):
    		""" <summary>
    		  Function to load a net assembly (over load of lisp version for .net)
    		 </summary>
    		"""
    		docThisDrawing = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
    		try:
    			strFullName = HostApplicationServices.Current.FindFile(strFileName, docThisDrawing.Database, FindFileHint.Default)
    			self.WriteMessage(strFullName + vbLf)
    			System.Reflection.Assembly.LoadFrom(strFullName)
    			return True
    		except System.Exception, Ex:
    			self.WriteMessage("Error Loading Assembly: " + Ex.Message + vbLf)
    		finally:
    		return False
    
    	def WriteMessage(self, strPrintString):
    		""" <summary>
    		 Simple print to command window function
    		 </summary>
    		"""
    		docThisDrawingP = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
    		docThisDrawingP.Editor.WriteMessage("" + strPrintString + vbLf + "")
    		return True
    		return False
    Also didn't mention any conversion problems (similar to Ruby), though I'd expect something along the same lines (Python & Ruby are slightly related).

    I've tried converting to the other built-in language "Boo", but it seems there are issues since Boo doesn't allow for the Option statement (Switch in C#):
    Code:
    8 conversion errors:
      Class.vb(72,9): BCE0000: old VB-style exception handling is not supported.
      My Project\Application.Designer.vb(11,1): BCE0000: Option statement is not supported.
      My Project\Application.Designer.vb(12,1): BCE0000: Option statement is not supported.
      My Project\Resources.Designer.vb(11,1): BCE0000: Option statement is not supported.
      My Project\Resources.Designer.vb(12,1): BCE0000: Option statement is not supported.
      My Project\Settings.Designer.vb(11,1): BCE0000: Option statement is not supported.
      My Project\Settings.Designer.vb(12,1): BCE0000: Option statement is not supported.
      My Project\Settings.Designer.vb(59,1): BCE0000: Only one namespace declaration per file is supported.
    Note, I've not yet tested the code. Will do so after work
    Attached Files Attached Files

  5. #5
    Certifiable AUGI Addict
    Join Date
    2015-11
    Location
    Jo'burg SA
    Posts
    4,512
    Login to Give a bone
    0

    Default Re: Netload Example

    Ok I modified that old version VB error handler to the newer try-catch version.
    Attached Files Attached Files

  6. #6
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,714
    Login to Give a bone
    0

    Default Re: Netload Example

    FWIW -

    Here are some I did, and Gile helped me to refine, some time ago:

    Netload and Dll LispFunctions

    Cheers
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

Similar Threads

  1. Error on Netload (DLL on network)
    By Dubweisertm in forum Dot Net API
    Replies: 2
    Last Post: 2011-09-22, 09:49 PM
  2. NETLOAD in AutoCAD 2012
    By LogoKevn in forum AutoCAD General
    Replies: 1
    Last Post: 2011-08-15, 06:54 PM
  3. Netload command not recognized
    By michael.hooker in forum Dot Net API
    Replies: 0
    Last Post: 2008-12-04, 09:29 PM
  4. VB.Net Netload DLL and Windows forms (EXE)
    By KevinBarnett in forum Dot Net API
    Replies: 2
    Last Post: 2005-12-15, 08:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •