Presentation is loading. Please wait.

Presentation is loading. Please wait.

Save Our Source! Converting VBA to VB.NET Mary West Senior Software Developer, Avatech Solutions.

Similar presentations


Presentation on theme: "Save Our Source! Converting VBA to VB.NET Mary West Senior Software Developer, Avatech Solutions."— Presentation transcript:

1 Save Our Source! Converting VBA to VB.NET Mary West Senior Software Developer, Avatech Solutions

2 Tools  AutoCAD version 2007, 2008 or 2009  Visual Basic 2005 Express Edition - free!  http://www.microsoft.com/express/2005/ http://www.microsoft.com/express/2005/  ObjectARX SDK – free!  www.autodesk.com www.autodesk.com

3 Base Application Setup  Launch Visual Basic 2005  New, Project, Class Library – enter a project name  Add References  Acdbmgd.dll  Acmgd.dll

4 Reference Properties Make sure both AutoCAD references have the “Copy Local” property set to False

5 Making Commands Import from Autodesk Define commands using the provided format

6 Setting up for DEBUG – Step 1 In the Project properties select “Debug” Next set the “Working directory” field to the location of the ACAD.EXE file

7 Setting up for DEBUG – Step 2 Edit MyTest.vbproj.user and add four lines, being the two and two lines as seen in the example here:

8 Export Code from VBA

9 Working with exported files Project, Add Existing Items, browse set file filter and select the exported file Delete any header information created by the export, such as: Attribute VB_Name = "AutomaticCenterLines“ Wrap it in a Public Class SomeClassName / End Class Add Imports of Autodesk classes VB2005 will suggest fixes for VB6 function and formatting changes

10 Framing.NET Functions Function AnyThatWorkWithDrawing() Dim docLock As DocumentLock = _ Application.DocumentManager.MdiActiveDocument.LockDocument() Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database Dim trans As Transaction = db.TransactionManager.StartTransaction() Try ‘Here is where you do all the work to drawing elements trans.Commit() ‘to keep the changes made Catch ex As Autodesk.AutoCAD.Runtime.Exception 'on error, the code here will be run trans.Abort() Finally 'this section will be run on both success and on failure. trans.Dispose() 'done with transaction docLock.Dispose() End Try End Function

11 Accessing Tables in VB.NET To get the equivalent of ThisDrawing.Layers, we use a Transaction.GetObject call to convert the drawing databases “LayerTableId” to a “LayerTable” object. If you have used HandleToObject in VBA, this works in a similar fashion. Dim lyrTbl As LayerTable = trans.GetObject(db.LayerTableId, _ OpenMode.ForRead, False) ThisDrawing.Linetypes is handled the same way: Dim ltypTbl As LinetypeTable = trans.GetObject(db.LinetypeTableId, _ OpenMode.ForRead, False)

12 InitializeLayer VBA Private Sub InitializeLayer(LayerName As String, LayerLType As String, LayerColor As Long) On Error Resume Next Dim myLayer As IAcadLayer Dim myLType As AcadLineType Set myLayer = ThisDrawing.Layers.Item(LayerName) If myLayer Is Nothing Then ' Create Layer Set myLayer = ThisDrawing.Layers.Add(LayerName) myLayer.Color = LayerColor Set myLType = ThisDrawing.Linetypes.Item(LayerLType) If myLType Is Nothing Then ‘If Linetype is not loaded, attempt to load it ThisDrawing.Linetypes.Load LayerLType, "acad.lin" Set myLType = ThisDrawing.Linetypes.Item(LayerLType) End If If Not myLType Is Nothing Then myLayer.Linetype = LayerLType End If End Sub

13 InitializeLayer VB.NET, part 1 Private Function InitializeLayer(ByVal LayerName As String, ByVal LayerLType As String, _ ByVal LayerColor As Long) As ObjectId Dim lyrId As ObjectId = Nothing Dim docLock As DocumentLock = _ Application.DocumentManager.MdiActiveDocument.LockDocument() Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database Dim trans As Transaction = db.TransactionManager.StartTransaction() Dim myLayer As LayerTableRecord Try 'Get the layer table object Dim lyrTbl As LayerTable = trans.GetObject(db.LayerTableId, OpenMode.ForRead, False) If lyrTbl.Has(LayerName) = False Then 'Create a new layer table record myLayer = New LayerTableRecord myLayer.Name = LayerName myLayer.Color = Color.FromColorIndex(ColorMethod.ByAci, LayerColor)

14 InitializeLayer VB.NET, part 2 Dim ltypTbl As LinetypeTable = trans.GetObject(db.LinetypeTableId, _ OpenMode.ForRead, False) 'If Linetype is not loaded, then load it If Not ltypTbl.Has(LayerLType) Then 'attempt to load it db.LoadLineTypeFile(LayerLType, "acad.lin") End If If ltypTbl.Has(LayerLType) Then Dim myLType As Object myLType = ltypTbl.Item(LayerLType) myLayer.LinetypeObjectId = myLType End If

15 InitializeLayer VB.NET, part 3 'We opened originally "ForRead", now we need to write lyrTbl.UpgradeOpen() lyrTbl.Add(myLayer) trans.AddNewlyCreatedDBObject(myLayer, True) lyrTbl.DowngradeOpen() End If ‘layer exists lyrId = lyrTbl.Item(LayerName) trans.Commit() 'do this to keep the changes we made Catch ex As Autodesk.AutoCAD.Runtime.Exception 'on error, the code here will be run trans.Abort() Finally 'this section will be run on both success and on failure. trans.Dispose() 'done with transaction docLock.Dispose() End Try Return lyrId End Function

16 New variable types/classes: Point3d Dim myPoint(0 to 2) As Double Dim myPoint As Point3d Point3d has over 25 Properties/Methods including: Add DistanceTo DivideBy IsEqualTo Origin Subtract Convert2d

17 New variable types/classes: Selection Filtering VBA Dim SSet As AcadSelectionSet Dim GroupCode(0 To 1) As Integer Dim DataValue(0 To 1) As Object Set SSet = _ ThisDrawing.SelectionSets.Add(“LineLyr”) 'Set up selection set filter GroupCode(0) = 0 DataValue(0) = “LINE“ GroupCode(1) = 8 DataValue(0) = “0“ SSet.SelectOnScreen GroupCode, DataValue VB.NET Dim SSet As PromptSelectionResult Dim filterVals(0 To 1) As TypedValue Dim myFilter As SelectionFilter 'Set up selection set filter filterVals (0) = New TypedValue _ (DxfCode.Start, “LINE") filterVals (1) = New TypedValue _ (DxfCode.LayerName, “0") myFilter = New SelectionFilter(filterVals) SSet = ed.GetSelection(myFilter)

18 Accessing Object Values If TypeOf (obj) Is Circle Or TypeOf (obj) Is Arc Then ‘use late binding to get the “Center” value of the Circle or Arc EndPoint1 = New Point3d(obj.Center.X, _ obj.Center.Y - LineLen, _ obj.Center.Z) If TypeOf (obj) Is Ellipse Then ‘cast to Ellipse to access “Center” Dim EllipseObj As Ellipse = obj EndPoint1 = PolarPoint(EllipseObj.Center, MajorRadiusAngle, LineLen)

19 Creating ModelSpace Entities VBA Set Cline = ThisDrawing.ModelSpace.AddLine(EndPoint1, EndPoint2) VB.NET blkTbl = trans.GetObject(doc.Database.BlockTableId, OpenMode.ForWrite, False) blkRec = trans.GetObject(blkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite, False) Cline = New Line(EndPoint1, EndPoint2) CLineId = blkRec.AppendEntity(Cline) trans.AddNewlyCreatedDBObject(Cline, True)

20 Creating Dialogs Project, Add New Item, Dialog The VB.NET Toolbox and Properties are similar to those in VBA

21 Resources  Autodesk Discussion Forums, see “AutoCAD Customization” http://discussion.autodesk.com/forums  AutoCAD User Group International (AUGI) forums. http://www.augi.com http://www.augi.com  Kean Walmsley of Autodesk has this website http://through-the-interface.typepad.com

22 Thank you! Please fill out the class evaluation I look forward to seeing you again next year!

23


Download ppt "Save Our Source! Converting VBA to VB.NET Mary West Senior Software Developer, Avatech Solutions."

Similar presentations


Ads by Google