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

Slides:



Advertisements
Similar presentations
Working with Profiles in IX1D v 3 – A Tutorial © 2006 Interpex Limited All rights reserved Version 1.0.
Advertisements

CP308-1L: Working with Actions and the Action Recorder Lee Ambrosius Autodesk, Inc Sr. Technical Writer.
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
AE6382 VBA - Excel l VBA is Visual Basic for Applications l The goal is to demonstrate how VBA can be used to leverage the power of Excel u VBA syntax.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
Advanced Microsoft Word Hosted by Jared Hoffman Topics Keyboard Shortcuts Customizing Toolbars and Menus Auto Format & Auto Correct Tabs Inserting Pictures.
CVEV 118/698 AutoCAD VBA Lecture 2 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.
CVEV 118/698 AutoCAD VBA Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.
Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with.
TM Getting Started with the Revit API Kristian Parsons Westfield Design & Construction.
Christopher Guertin VAMC – West Palm Beach, FL 1.
Create Forms Lesson 5. Software Orientation Creating Forms A form is a database object –enter, edit, or display data from a table or query Providing.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Visual Basic 2008 Express Edition The IDE. Visual Basic 2008 Express The Start Page Recent Projects Open an existing project Create a New Project.
Database-Driven Web Sites, Second Edition1 Chapter 8 Processing ASP.NET Web Forms and Working With Server Controls.
Back to content Final Presentation Mr. Phay Sok Thea, class “2B”, group 3, Networking Topic: Mail Client “Outlook Express” *At the end of the presentation.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Intellicad Visual Basic Application Marian Kate Santos Programming Paradigm.
CS Tutorial 1 Getting Started with Visual Studio 2012 (Visual Studio 2010 are no longer available on MSDNAA, please choose Visual Studio 2012 which.
Chapter 9 Files I/O: Files, Records and Fields Part 3.
1- Date TimePicker 2- Month Calendar 3- User Defined Controls.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
© Minder Chen, ASP.NET 2.0: Introduction - 1 ASP.NET 2.0 Minder Chen, Ph.D. Framework Base Class Library ADO.NET: Data & XML.
CARLSON SOFTWARE CONFERENCE DANIEL V. SYPERSMA VICTOR GRAPHICS.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
Automating Database Processing Chapter 6. Chapter Introduction Design and implement user-friendly menu – Called navigation form Macros – Automate repetitive.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Thirteen Working with Access Databases and LINQ.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
SYSTEMSDESIGNANALYSIS 1 OO: Chapter 9 Visual Basic: Building Components Jerry Post Copyright © 1999.
You can use Access forms to create an interface to your reports and queries. You can add: Buttons to initiate reports and queries Combo Boxes, List.
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
1 Chapter 12 – Web Applications 12.1 Programming for the Web, Part I 12.2 Programming for the Web, Part II 12.3 Using Databases in Web Programs.
Microsoft Visual Basic 2008 CHAPTER TWELVE Cell Phone Applications and Web Services.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Damian Tamayo Tutorial DTM Data Generator Fall 2008 CIS 764.
1 CS 3870/CS 5870: Note 07 Lab 3 Lab 4 Test 1: Two Tables.
Chapter 3 The Visual Basic Editor. Important Features of the VBE Alt-F11 will open the Visual Basic Editor. The Code window is to the right, Project Explorer.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 8 Debugging, Creating Executable Files, and Distributing a Windows Application.
Course ILT Forms and queries Unit objectives Create forms by using AutoForm and the Form Wizard, and add or modify form headers and footers Open and enter.
ADO.NET Objects – Data Providers Dr. Ron Eaglin. Requirements Visual Studio 2005 Microsoft SQL Server 2000 or 2005 –Adventure Works Database Installed.
JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.
By Paul Richard and Jim Fitzgerald Chapter 18 – Drawing Management Tools and Utilities.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge
INSERT BOOK COVER 1Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Getting Started with VBA for Microsoft Office 2010 by.
1 Visual Basic Part I - A tool for customizing your program Principles of GIS
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 25 I’m Suffering from Information Overload.
Chapter 24 I’m Suffering from Information Overload (Access Databases) Clearly Visual Basic: Programming with Visual Basic nd Edition.
ADO.NET Objects Data Adapters Dr. Ron Eaglin. Agenda Builds on Information in Part I Should have working knowledge of creating a database connection Continuation.
CIS 338: VB.NET Printing Dr. Ralph D. Westfall May, 2011.
Integrating and Extending Workflow 8 AA301 Carl Sykes Ed Heaney.
1 Adding a Model. We have created an MVC web app project Added a controller class. Added a view class. Next we will add some classes for managing movies.
Visual Basic Fundamental Concepts
Intellicad Visual Basic Application
Programming in visual basic .net Visual Basic Building Blocks
VBA - Excel VBA is Visual Basic for Applications
VBA - Excel VBA is Visual Basic for Applications
Computer Programming I
Introduction to VB6 Week 1 3/2/2004 PPCC - Introduction to VB6
Using Procedures and Exception Handling
Excel VBA Day 3 of 3 Tom Vorves.
Design and Consume DataWindows in Visual Studio 2005
Visual Basic..
Adding Record To Your Database
Accessing Databases with ADO.NET, Handling Exceptions, and Printing
Lecture Set 9 Arrays, Collections, and Repetition
Presentation transcript:

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

Tools  AutoCAD version 2007, 2008 or 2009  Visual Basic 2005 Express Edition - free!   ObjectARX SDK – free! 

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

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

Making Commands Import from Autodesk Define commands using the provided format

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

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:

Export Code from VBA

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

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

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)

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

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)

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

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

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

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)

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)

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)

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

Resources  Autodesk Discussion Forums, see “AutoCAD Customization”  AutoCAD User Group International (AUGI) forums.  Kean Walmsley of Autodesk has this website

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