PDA

View Full Version : Netload Example



peter
2014-01-03, 10:59 PM
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.





'
' 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

peter
2014-01-05, 12:11 AM
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.

http://forums.augi.com/attachment.php?attachmentid=94531&stc=1

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.

http://forums.augi.com/attachment.php?attachmentid=94527&stc=1c\

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.

http://forums.augi.com/attachment.php?attachmentid=94528&stc=1

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.

http://forums.augi.com/attachment.php?attachmentid=94529&stc=1

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.



http://forums.augi.com/attachment.php?attachmentid=94530&stc=1

After selecting the add button. You can select the browse tab to find the acdbmgd.dll etc... files and add them to your reference.

irneb
2014-01-14, 05:52 AM
As requested, here's the C# of this code - converted using SharpDevelop's one-click project converter (full cs project attached):
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:
1 error(s) converting C:\####\NetLoad\NetLoad\Class.vb:
-- line 72 col 9: Not supported in C#: OnErrorStatementThat 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.

irneb
2014-01-14, 06:00 AM
And then just for fun :mrgreen: ... 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
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
endAnd then the Python version:
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 FalseAlso 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#):
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 ;)

irneb
2014-01-14, 06:13 AM
Ok I modified that old version VB error handler to the newer try-catch version.

BlackBox
2014-01-14, 09:26 PM
FWIW -

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

Netload and Dll LispFunctions (http://www.theswamp.org/index.php?topic=41639.msg467429#msg467429)

Cheers