1 / 15

CVEV 118/698 AutoCAD VBA

CVEV 118/698 AutoCAD VBA. Lecture 2. Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar. Application Methods. The application is accessed either by a reference name chosen by the programmer upon declaration or by the “ThisDrawing.Application.” syntax.

langer
Download Presentation

CVEV 118/698 AutoCAD VBA

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CVEV 118/698 AutoCAD VBA Lecture 2 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar

  2. Application Methods • The application is accessed either by a reference name chosen by the programmer upon declaration or by the “ThisDrawing.Application.” syntax. • Be cautious when using application methods for they can affect the whole AutoCAD window and all its documents. • Methods: • Quit: exits drawing and drawing editor. • RunMacro: runs VBA application. • Update: updates the object on the display. • Zoom… many methods available for zooming

  3. Application Properties • Many of the properties are read-only to keep the system integrity level high. • Properties: • FullName: returns the complete path and file name. • Caption: displays the caption above the primary window of AutoCAD (read-only property). • ActiveDocument: points to the active document (equivalent to “ThisDrawing.”. • Documents: provides access to the collection of documents opened in the AutoCAD application. • Preferences: provides access to drawing setup. • Etc.

  4. Application Properties Example Dim oAcad As AcadApplication Dim oDoc As AcadDocument Set oAcad = GetObject(, "AutoCAD.Application") Set oDoc = oAcad.ActiveDocument oAcad.Preferences.Display.CursorSize = 5 oAcad.Documents.Open (strDocumentName) oDoc.Activate

  5. Document Methods • Methods: • New: creates a new document. • Save: saves changes to the current drawing. • Open: opens an existing drawing in single document mode. For multiple document mode, use it with “Documents” collection, I.e. ThisDrawing.Application.Documents.Open(Name) • Close: closes the drawing. • Activate: makes the document active.

  6. Layers • “Layers” is a collection directly linked to the Document object (see Object Model). • Although this can be done at the individual level of the object, it is advised to manage all color, line types, etc. settings from the layers of the “Layers” collection. • Changing those settings can be done directly using a single layer’s declaration name or by referring to its position in the Object Model.

  7. Layers Example Dim LayFrame As AcadLayer Set LayFrame = ThisDrawing.Documents.Add(LayFrame) LayFrame.Color = acBlue set LayFrame = ThisDrawing.ActiveLayer For i = 0 To ThisDrawing.Layers.Count – 1 If LayFrame = ThisDrawing.Layers.Item(i) Then Exit For End If Next i ThisDrawing.Layers.Item(i).Linetype = StrLineTypeName

  8. Model Space & Paper Space • Model Space and Paper Space are the Primary Spaces of the AutoCAD application. • Model Space: a geometric model placed in a 3D coordinate space. • Paper Space: used for creating a finished layout for printing or plotting. • Objectsare only made visible when stored on either of the two Primary Spaces Collections.

  9. Model Space & Paper Space (cont’d) • Referencing and adding objects in model space and paper space can be done in similar ways as for regular collections (Item( ), Count, Delete, Add*,etc…). • More methods are supported by the two primary spaces, mainly functions for adding entities to the collections. • However, the “Remove” function is not available; deleting an object from the primary spaces is done using the “Erase” method. • Unlike regular Selection Sets, upon exit of the VBA application, the information stored in the primary spaces collections is kept as a database for the drawing.

  10. Utility Object Functions • The Utility object is directly linked to the Document object in the object model. • It contains functions only, I.e. no data properties. • It provides with many functions to convert data from one format to another. • It also provides with tools to support user input and interaction via the traditional AutoCAD interface methodologies (I.e. Command Line).

  11. Utility Object Functions • Some Conversion Methods: • RealToString / StringToReal • AngleToReal /AngleToString • PolarPoint: returns point variant determined relative to an existing point. • Some User Interactive Methods: • Promt (No return) • GetReal / GetInteger / GetString / GetAngle • GetPoint (selection on screen, return variant) • GetEntity (selection of object on screen, returns object)

  12. Utility Object Functions (Cont’d) Dim i As Integer : Dim Nlines As Integer : Dim strNum As String Dim dblStPnt(0 To 2) As Double : Dim dblEnPnt(0 To 2) As Double Dim acLine() As AcadLine With ThisDrawing.Utility Nlines = .GetInteger("Enter number of lines to draw:") ReDim acLine(Nlines - 1) As AcadLine For i = 1 To Nlines strNum = .RealToString(i, acDecimal, 0) dblStPnt(0) = .GetReal("Enter st pnt x for line " + strNum +":") dblStPnt(1) = .GetReal("Enter st pnt y for line " + strNum +":") : dblStPnt(2) = 0# dblEnPnt(0) = .GetReal("Enter End pnt x for line " + strNum +":") dblEnPnt(1) = .GetReal("Enter End pnt y for line " + strNum +":") : dblEnPnt(2) = 0# Set acLine(Nlines - 1) = ThisDrawing.ModelSpace.AddLine(dblStPnt, dblEnPnt) Next i End With

  13. Error Handler • User interaction increases the risk of having errors at run time • The user could for example select nothing, while requested to select an object . This might cause a crash in the application. • For this reason, it is necessary to set up an error handler in the code, where user input is requested.

  14. Error Handler Dim dblPnt(0 To 2) As Double Dim objSelect As Object Again: On Error Resume Next ThisDrawing.Utility.GetEntity objSelect, dblPnt, "Pick Object:" If Err.Number <> 0 Then Err.Clear MsgBox "No entity was selected" GoTo Again End If

  15. What’s Next • VBA lab • Filtering the selection set • The True Story of PaperSpace

More Related