0% found this document useful (0 votes)
32 views213 pages

01 CCCS 14 Aug

Uploaded by

2020bme067
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views213 pages

01 CCCS 14 Aug

Uploaded by

2020bme067
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 213

SHRI GURU GOBIND SINGJI INSTITUTE OF

ENGINEERING AND TECHNOLOGY, NANDED.


(An Autonomous Institute of Government of Maharashtra)

R. U. Kalyankar
Department of Mechanical Engineering
Class : FY M.Tech 2020-21
Semester : II
Course: Customization of CAD/CAM Software (CCCS)
Course Code: (PEC-PL 516 & PCC-DM-512)
Course Coordinator: Prof. R. P. Parvekar
Mr. R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 1
Customization of CAD/CAM Software (CCCS) (PEC-PL 516 & PCC-DM-512)

• What is CCCS?
• What is Customization?
Customization – Tailor-made
• The CAD/CAM software available in the market are general purpose software
(targets a broad set of requirements).

R. U. Kalyankar
• These software are not developed for particular user or particular task.
• The process of molding of the software for particular application to suite the
specific requirements of the customer/user is called customization of software.
• Custom software development is the process of designing, creating, deploying and
maintaining software for a specific set of users, functions or organizations.

8/14/2021 SGGSIE&T, Nanded CCCS 2


Course Objectives:
1. Add more features and function to the existing command, CAD template
designing, CAE analysis template customization.
2. To make aware Interface customization, e. g icon/menu, naming and
arrangement.
3. Comprehend AutoCAD/CATIA and VBA customization techniques.

R. U. Kalyankar
4. Inculcate the API in UG/NX and AutoCAD.

8/14/2021 SGGSIE&T, Nanded CCCS 3


Course Outcomes:
1. Identify custom software development requirements related to CAD, CAM
applications
2. Design and develop the following for custom tool development in CAD software
like NX, CATIA, AutoCAD, AIP, etc.
i. User friendly and complete UIs

R. U. Kalyankar
ii. Algorithms and programs for modeling parts
3. Design and develop custom software for CAM applications using DBMS like
MS access, VFP, Oracle.

8/14/2021 SGGSIE&T, Nanded CCCS 4


Course Content:
• Unit I: Introduction To CCCS:
• What is Customization, Need of CAD/CAM Customization, History, Part Library
and Part Family in CAD, User Defined Function (UDF), Creation of Custom
Templates, Macros, Linetypes and Hatching Patterns, Script Programming.
• Unit II - UI Customization for CCCS

R. U. Kalyankar
• AutoCAD & NX UI Customization
• Unit III - Introduction to Programming for CCCS
• IDEs, APIs in CAD Software, General Purpose and System Dependent API,
Projects - Sample Projects, Objects, Methods And Properties, AutoCAD Object
Tree, Data types.

8/14/2021 SGGSIE&T, Nanded CCCS 5


Course Content:
• Unit IV - Programming for Modeling (2D/3D)
• Creating 2D and 3D entities through Programming; Editing 2D and 3D entities
through Programming.
• Unit V - UI Design/Development
• Forms Design, Events, User Interactions and Utility objects, Controlling Menus,

R. U. Kalyankar
Toolbars, Layers and Linetypes.
• Unit VI: Additional Topics in Programming for CCCS
• Dimensions, Annotations, Layout, Creating Tables, CAM Applications
development using DBMS and SQL Programming.

8/14/2021 SGGSIE&T, Nanded CCCS 6


Reference Books
Textbooks / Documentation:
• Autodesk AutoCAD VBA Developer Documentation Help.
• GRIP programming manuals for Unigraphics (Vol. 1 and 2).
Reference Books / Documentation:
• Joe Sutphin, AutoCAD 2006 VBA: A Programmer's Reference, Apress; 2nd ed.

R. U. Kalyankar
edition (September 23, 2005).
• Marion Cottingham, Mastering AutoCAD VBA, Sybex; 1 edition (March 16,
2001).
• Lee Ambrosius, AutoCAD Platform Customization: User Interface, AutoLISP,
VBA, and Beyond, Sybex; 1 edition (April 27, 2015).
• Sham Tickoo, Customizing AutoCAD 2020, 13th Edition, CADCIM Technologies
(September 11, 2019).
• User Function Programming manuals for Unigraphics (Vol. 1, 2, 3).

8/14/2021 SGGSIE&T, Nanded CCCS 7


Pre-requisites for the Course
• Proficiency in CAD/CAM Software
• Basic programming knowledge (C/C++/VB etc.)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 8
Lectures and ISE – I & II
CCCS (PEC-PL 516) – Credits – 3 + 1
• Contact Hours - Th. (2 hrs. RUK + 1 hr. RPP) & Pr. (2 hrs. RUK)
CCCS (PCC-DM-512) – Credits – 3 + 2
• Contact Hours - Th. (2 hrs. RUK + 1 hr. RPP) & Pr. (2 hrs. RUK + 2 hrs.
RPP)

R. U. Kalyankar
ISE-I (20 Marks Before Mid)
• Presentations on syllabus topics
• Assignment's presentations
• Quizzes
ISE-II (20 Marks After Mid)
• Presentations on syllabus topics
• Assignment's presentations
• Quizzes

8/14/2021 SGGSIE&T, Nanded CCCS 9


Practical- continuous evaluation
• Practical/ Lab work
• At least six Practical assignments on CCCS syllabus.

• Software list, downloads, help,


• Autodesk AutoCAD,

R. U. Kalyankar
• Autodesk Inventor Professional
• https://www.autodesk.com/education/edu-
software/overview?sorting=featured&page=2
• Opensource and free for education Software

8/14/2021 SGGSIE&T, Nanded CCCS 10


NPTEL, SWAYAM, etc course registrations, if any

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 11
Expectations from students and feedback related to Semester-I

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 12
Script Programming
Learning Objectives:
• Write script files and use the SCRIPT command to run script files.
• Use the RSCRIPT and DELAY commands in script files.
• Invoke script files when loading AutoCAD.
• Create a slide show.

R. U. Kalyankar
• Preload slides when running a slide show.

8/14/2021 SGGSIE&T, Nanded CCCS 13


Script Programming (Cont…..)
What are script files?
• AutoCAD has provided a facility called script files that allows you to combine
different AutoCAD commands and execute them in a predetermined sequence.
Uses:
• Script files can be used to generate slide show,

R. U. Kalyankar
• do the initial drawing setup,
• or plot a drawing to a predefined specification.
• Also used to automate certain command sequences that are used frequently in
generating, editing, or viewing a drawing.
• Script cannot access dialog boxes or menus.
• When commands that open the file and plot dialog boxes are issued from script
file, AutoCAD runs the command line version of the command instead of opening
the dialog box.

8/14/2021 SGGSIE&T, Nanded CCCS 14


Script Programming (Cont…..)
How to write the code?
Which editor should be used?
What should be the extension/name of program file?
Which editor should be used?
• Editor → Any text editor (Notepad/WordPad)

R. U. Kalyankar
What should be the extension/name of program file?
• .scr/filename.scr
How to write the code?
LINE
0,0
5,0

8/14/2021 SGGSIE&T, Nanded CCCS 15


Script Programming (Cont…..)
How to write the code?
• What is command prompt in AutoCAD? How it works?
• Before writing the script file, you need to know the AutoCAD commands and the
entries required in response to the command prompts.
• For that, you can type command at the keyboard and then respond to different

R. U. Kalyankar
prompts.
• E.g.
• ORTHO
• GRID
• SNAP
• LIMITS
• ZOOM
8/14/2021 SGGSIE&T, Nanded CCCS 16
Script Programming (Cont…..)
How to write the code?
Ex.01
ORTHO ON
;space is used to terminate a command or a prompt entry
;ORTHO ; comment line

R. U. Kalyankar
;ON same as line 01
GRID 2.0 GRID OFF
SNAP 0.5 SNAP ON
LIMITS 0,0 50,50
ZOOM ALL
SETVAR TEXTSIZE 0.125
LTSCALE 4.0
SETVAR DIMSCALE 4

8/14/2021 SGGSIE&T, Nanded CCCS 17


Script Programming (Cont…..)
SCRIPT Command
• Executes a sequence of
commands from a script file.

• The Select Script File dialog box

R. U. Kalyankar
(a standard file selection dialog
box) is displayed. Enter the file
name of a script to run that
script.

8/14/2021 SGGSIE&T, Nanded CCCS 18


Script Programming (Cont…..)
How to write the code?
Ex.02
;Script file that will rotate the circle and the line, around the base point of the line through 45-degree increments
;LINE 4.25,6.5 5.0,7.0
;
;CIRCLE 5.0,7.0 0.15
ROTATE
;Rotates the objects
W

R. U. Kalyankar
;Window option for selecting the objects that need to be edited
2.25,5.0
;Lower left corner
6.25,9.0
;Upper right corner

;Blank line that terminates the object selection process


4.25,6.5
;Base point for rotation
5
;incremental angle for rotation
DELAY 1000
;Time delay in milliseconds
RSCRIPT

;Repeat indefinitely until cancel

8/14/2021 SGGSIE&T, Nanded CCCS 19


Limitations
• No variables, arrays, etc

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 20
Unit III - Introduction to Programming for CCCS
• IDEs,
• APIs in CAD Software,
General Purpose and System Dependent API,
• Projects - Sample Projects,
• Objects, Methods And Properties,

R. U. Kalyankar
• AutoCAD Object Tree,
• Data types.

8/14/2021 SGGSIE&T, Nanded CCCS 21


Introduction to API
• An API is a set of functions, procedures, methods, or classes used by computer
programs to request services from OS, software libraries, or any other service
providers running on the computer.
• A computer programmer uses the API to make application program.
• By providing an API, software allows you to add additional functionality and

R. U. Kalyankar
optimize repetitive operations to make it more productive for your individual
needs.

8/14/2021 SGGSIE&T, Nanded CCCS 22


Introduction to API (Cont…)
• The API normally comprises a library of procedures that may be used for variety
of system functions such as:
➢Access the data values for model entities.
➢Add, modify and delete geometric entities.
➢Manage part and other files.

R. U. Kalyankar
➢Access and set system values such as line-style and colour.
➢Create views.
➢Interact with the operator.
➢Output non graphical data to the graphics screen.
➢Dimension drawings.

8/14/2021 SGGSIE&T, Nanded CCCS 23


Types of API
• There are two types of API:
1.System or Software Dependent API
2.General Purpose API
1. System or Software Dependent API:
➢The programming language that used for particular CAD/CAM package is called

R. U. Kalyankar
as System or Software Dependent API.
eg. AutoLISP for AutoCAD;
GRIP for UGNX;
Pro-Programming for Creo.
2. General Purpose API:
➢Widely used programming language that are common to every CAD/CAM
software, are called General Purpose API.
eg. C, C++, C#, Java, Visual Basic, Visual C++, Python, KF, etc
8/14/2021 SGGSIE&T, Nanded CCCS 24
Various APIs available in CAD software
Various APIs available in NX:
➢NX Open GRIP (SD)
➢NX Open C (GP)
➢NX Open C++ (GP)
➢NX Open Java (GP)

R. U. Kalyankar
➢NX Open .NET (C#) (GP)
➢NX Open Python (GP)
➢SNAP - Simple NX Application Programming (VBA) (GP)

➢KF (Knowledge Fusion) programming module based on C++ API (GP)

8/14/2021 SGGSIE&T, Nanded CCCS 25


Various APIs available in CAD software (Cont…..)
Various APIs available in CATIA:
➢CATIA VBA
➢C++ Automation API
Programming Module:
➢Knowledge Expert (KE)
➢Knowledge Adviser (KA)

R. U. Kalyankar
Various APIs available in AutoCAD:
➢AutoCAD VBA
➢AutoLISP (SD)
➢Visual LISP
➢ C API
➢C++ API
➢JavaScript
➢managed.NET

8/14/2021 SGGSIE&T, Nanded CCCS 26


UI: Starting the Editor
• One of the first questions that you will face is “How do I enter source code
(structured commands) or develop a user interface (forms or dialog boxes)?”
• The answer is the IDE, a graphical user interface you use to develop applications.

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 27
The VBAIDE
• VBA projects must be loaded into the VBA development environment before they
can be edited or used.
• Editing a VBA project is handled in the Visual Basic for Applications Integrated
Development Environment, or commonly known as the VBAIDE.

R. U. Kalyankar
• The VBAIDE is displayed by clicking Manage tab ----> Applications panel --->
Visual Basic Editor on the ribbon or entering VBAIDE at the AutoCAD Command
prompt.
• The VBAIDE contains a number of tools that are used to add and modify the
components of a loaded VBA project.

8/14/2021 SGGSIE&T, Nanded CCCS 28


The VBAIDE (Cont….)
• The following
illustration calls
out the main
components of the
VBAIDE you will
want to become

R. U. Kalyankar
familiar with:

8/14/2021 SGGSIE&T, Nanded CCCS 29


The VBAIDE (Cont….)
• Project Explorer – Provides access to the components of all loaded VBA
projects. These components include standard and class modules, and UserForms.
• Properties window – Provides the ability to view and modify the properties of a
selected component from the Project Explorer or an object selected in the form
editor window when editing a UserForm.

R. U. Kalyankar
• Editor window – Provides access to the code of a component in a loaded VBA
project or the form editor for a UserForm.
• Toolbox window – Provides access to the controls that can be placed on a
UserForm.
• Object Browser – Provides a view into the libraries that are currently referenced
to a VBA project.

8/14/2021 SGGSIE&T, Nanded CCCS 30


Designing a UserForm
• The UserForm1 object is the canvas upon which you visually design your
application, and it provides the windows, your users interact with when they run
the application.
• UserForms have their own properties, methods, and events you can use to control
their appearance and behavior.

R. U. Kalyankar
• The first step in designing a UserForm is to set its properties, such as width and
height.
• You may set a UserForm’s properties at design time or at run time using code also.

8/14/2021 SGGSIE&T, Nanded CCCS 31


Adding a UserForm to Your Application

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 32
Setting UserForm Properties
• UserForm properties control a form’s physical
appearance.
• The UserForm.Caption property defines the
text that appears on the left-hand side of the
UserForm title bar, as shown in Figure
• The UserForm.Height and UserForm.Width

R. U. Kalyankar
properties control the initial UserForm height
and width, respectively,
• However, you may change these properties at
run time as demonstrated in the following
• example:
Private Sub UserForm_Activate()
UserForm1.Width = 200
UserForm1.Height = 150
End Sub

8/14/2021 SGGSIE&T, Nanded CCCS 33


Adding a Control to a Form
• When you add a UserForm to your project, another window immediately appears.
• This window, called the Toolbox, is where you’ll find the intrinsic, or default, ActiveX controls
that are available for you to design your user interface.
• Placing an ActiveX control on a form is simply a matter of clicking the control in the Toolbox and
then clicking the area of the UserForm where you want to place the control.
• Each time you drop a new control onto a form, VBA automatically provides a default name and

R. U. Kalyankar
index number for the control.
• For example, the first TextBox control will be named TextBox1 by default. You can rename
controls by modifying their Name property.

8/14/2021 SGGSIE&T, Nanded CCCS 34


Class Controls and Their Property Settings

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 35
Code
• Next, you’ll enter some code and see how to make this dialog box interface work. First, you need
to open the UserForm code module by selecting View ➤ Code or pressing F7.
• Or you may start by double-clicking the < Pick … command button and entering the code listed
here:
Private Sub cmdPick_Click()
Dim Point As Variant
On Error Resume Next

R. U. Kalyankar
'hide the UserForm
UserForm1.Hide
'ask user to select a point
Point = ThisDrawing.Utility.GetPoint(, "Select a point")
If Err Then Exit Sub
'assign values to appropriate textbox
txtX = Point(0): txtY = Point(1): txtZ = Point(2)
'redisplay the UserForm
UserForm1.Show
End Sub
8/14/2021 SGGSIE&T, Nanded CCCS 36
Run Sub/UserForm
• This is all the code you need to make this example work. Execute this example by
selecting Run ➤ Run Sub/UserForm or by pressing F5.
• Alternatively, you can run the example by clicking the Run toolbar button as
shown in Figure.
• This final bit of code allows you to exit your application gracefully:

R. U. Kalyankar
Private Sub cmdOK_Click()
Unload Me
End Sub

The VBAIDE Run, Pause, and Stop buttons

8/14/2021 SGGSIE&T, Nanded CCCS 37


ToolBox Controls
• The AutoCAD VBA environment provides you with 14
toolbox controls.
• Label
• Used to convey information back to the user, the Label
control looks like what you see in Figure when you

R. U. Kalyankar
place it on a UserForm.

8/14/2021 SGGSIE&T, Nanded CCCS 38


ToolBox Controls (Cont…..)
• TextBox
• You use TextBox controls for data entry by the user
or to show the data to the user.
• ComboBox
• The ComboBox control, an example of which

R. U. Kalyankar
appears in Figure allows a selection from a standard
group of possible responses presented in a drop-
down list.

8/14/2021 SGGSIE&T, Nanded CCCS 39


ToolBox Controls (Cont…..)
• ListBox
• The ListBox control, shown in Figure, allows a selection from a standard group of
possible responses.
• The difference between the ListBox control and the ComboBox control is the
ListBox control displays more than a single choice at a time in a list format.

R. U. Kalyankar
• Also, you can’t type values into a ListBox control.

8/14/2021 SGGSIE&T, Nanded CCCS 40


ToolBox Controls (Cont…..)
• CheckBox
• Use the CheckBox control to determine if an item is selected.
• If the box is unchecked, then the item isn’t selected.
• If the box is checked, then the user has selected that item.

R. U. Kalyankar
You may have any number of CheckBox controls on a UserForm; each CheckBox
control’s checked state is independent of any other CheckBox control.

8/14/2021 SGGSIE&T, Nanded CCCS 41


ToolBox Controls (Cont…..)
• OptionButton
• The OptionButton control is ideal for situations in which you want your user to
choose just one item.
• Most AutoCAD users know these as radio buttons.
• The OptionButton control is usually placed inside a Frame control for grouping.

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 42
ToolBox Controls (Cont…..)
• ToggleButton
• Use a ToggleButton control to enable an option and leave it that way until the user
depresses the button again.
• This control presents an on/off switch whose appearance changes depending on
whether or not the user has depressed it.

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 43
ToolBox Controls (Cont…..)
• Frame
• The Frame control, shown in Figure, is a container for other controls, similar to
the UserForm.
• Controls placed within a Frame control will move when you move the Frame
control.

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 44
ToolBox Controls (Cont…..)
• CommandButton
• Use CommandButton controls, shown in Figure, to allow users to signify that they
have made all the selections and text entries they want to make, and either want to
continue on with the application or want to cancel the operation.
• The most common use of this control is for Ok and Cancel button operations.

R. U. Kalyankar
• The following is the most common method of ending a running application:
Private Sub cmdOK_Click()
Unload Me
End Sub

8/14/2021 SGGSIE&T, Nanded CCCS 45


ToolBox Controls (Cont…..)
• TabStrip
• Use TabStrip controls when the data format between each tab is the same, but each
tab represents a different entity.

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 46
ToolBox Controls (Cont…..)
• MultiPage
• The MultiPage control, shown in Figure, is similar to a UserForm.
• Each tab or page is separate from the others.
• For example, if you add a control to Page1, the same control doesn’t appear on
any other page. Nor is it a part of the UserForm.

R. U. Kalyankar
• The MultiPage control is similar to a Frame control in that when you move the
MultiPage control, all the controls contained within it move as well.

8/14/2021 SGGSIE&T, Nanded CCCS 47


ToolBox Controls (Cont…..)
• ScrollBar
• Use ScrollBar controls to allow the user to
easily change values based on clicking the
up and down arrows of the control.
• Typically, a ScrollBar increases or

R. U. Kalyankar
decreases counting values within a TextBox
control.
• This control contains a slider bar indicating
its relative position to the minimum and
maximum values.
• The slider also allows the user to quickly
make very large changes to the value.

8/14/2021 SGGSIE&T, Nanded CCCS 48


ToolBox Controls (Cont…..)
• SpinButton
• A SpinButton control allows the user to change values easily based on clicking the
up and down arrows of the control.

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 49
ToolBox Controls (Cont…..)
• Image
• The Image control, shown in Figure, enables you to display pictures on your
UserForm.
• The following types of images are supported: *.bmp, *.cur, *.gif, *.ico, *.jpg, and
*.wmf.

R. U. Kalyankar
• With the Image control’s default event being Click, you could also use it as a
fancy CommandButton control with a picture.

8/14/2021 SGGSIE&T, Nanded CCCS 50


VBA Macro
• A macro is a block of code statements that perform a particular task.
• The task might be as simple as displaying a dialog box to remind users to update
something on their drawing,
• or it might be starting up a VBA application.
• Macros are normally quite short and contain only a few lines of code.

R. U. Kalyankar
• Macros are great relief for those boring repetitive jobs we all have to do from time
to time.

8/14/2021 SGGSIE&T, Nanded CCCS 51


Project Structure
• A VBA project contains several different types of files, including the following:
• UserForm module
• Standard module
• Class module
• Reference .dvb file
• UserForm module

R. U. Kalyankar
• UserForm modules (files with a .frm extension) contain a text description of your form,
controls placement, and property settings.
• They also include UserForm-level declarations of constants, variables, and procedures;
and event procedures.
• Standard Module
• Standard modules (files with a .bas extension) contain module-level declarations of
programmer defined data types, constants, variables, and public procedures.
• A standard module typically contains routines that don’t fit nicely into a class definition.

8/14/2021 SGGSIE&T, Nanded CCCS 52


Project Structure
Class Module
• Use class modules (files with a .cls extension) to create your own objects,
including methods, properties, and events.
• Class modules are similar to UserForm modules except that they have a visible
user interface.

R. U. Kalyankar
Reference .dvb File (Project File)
• You can reference the code of another .dvb file in your current project.
• This feature lets you easily reuse code among several projects.

8/14/2021 SGGSIE&T, Nanded CCCS 53


AutoCAD VBA Project Terms Reference
• Terms related to VBA projects and the VBA IDE.
• Global Project
• A VBA project stored in a .dvb file.
• Embedded Project

R. U. Kalyankar
• A VBA project stored in an AutoCAD drawing.
• Regular Document
• An AutoCAD drawing that does not contain VBA embedded projects.
• Smart Document
• An AutoCAD drawing that contains one or more VBA embedded projects.
• Current Project
• The project currently selected in the VBA IDE.

8/14/2021 SGGSIE&T, Nanded CCCS 54


AutoCAD VBA Project Terms Reference (Cont...)
ThisDrawing
• ThisDrawing is a VBA programming term used to represent the current drawing.
• For global projects, ThisDrawing always refers to the active document in AutoCAD.
• For embedded projects, ThisDrawing always refers to the document containing the project.
VBA IDE

R. U. Kalyankar
• This application allows you to edit the code and forms in your project or copy code and forms from
other projects.
• It also allows you to set references to other application Object Models.

8/14/2021 SGGSIE&T, Nanded CCCS 55


AutoCAD VBA Project Terms Reference (Cont...)
VBA Manager
• The VBA Manager allows you to manage your projects.
• You can create, delete, embed, or extract projects.
• You can also view which projects, if any, are embedded in an open drawing.

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 56
AutoCAD VBA Project Terms Reference (Cont...)
Macros Dialog Box
• The Macros dialog box allows you to run, delete, and create new macros, and provides access to
the VBA project options.

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 57
Finding Help on AutoCAD VBA

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 58
Overview of Objects, Properties, Methods, and Events

• VBA is an object-based programming language, containing many different classes of


objects.
• Each class has its own set of properties to define the appearance of the objects, and
methods to manipulate the objects that are instances of that class.
• Some classes, such as ToolBox controls, also have their own set of event procedures

R. U. Kalyankar
that are coded with the application’s response to user events.

8/14/2021 SGGSIE&T, Nanded CCCS 59


Overview of Objects, Properties, Methods, and Events (Cont…)
• All about Objects:
• Everything relating to an object is defined in its class.
• There are two main groups of AutoCAD objects:
• Drawing Objects: Drawing objects represent anything that has been drawn in the
Model Space tab of the AutoCAD window.
• Even when you draw a simple line, AutoCAD creates an object to store all the details

R. U. Kalyankar
needed to regenerate it.
• UserForm Objects: UserForm objects represent anything that’s included in the
graphical user interface (GUI).
• So, each time you drag and drop a control from the Toolbox, an object is created that
is an instance of that class of control.
• Objects are said to be instances of classes, and their properties, methods, and events
are said to be members of that class.

8/14/2021 SGGSIE&T, Nanded CCCS 60


Variables
• Variables let you retain values for use locally in a procedure or globally in your
entire application.
• Visual Basic refers to variables by name, such as InsertionPoint or LayerName.
• It’s best to avoid using variable names that coincide with predefined property or
event names.

R. U. Kalyankar
• Variables are known by their data type, that is, the kind of data that they can store.
• The data type determines the amount of memory required to store the variable’s
value.

8/14/2021 SGGSIE&T, Nanded CCCS 61


Variables (Cont….)
Declaring Variables
• Use the Dim statement in a procedure to declare a variable. Here’s the syntax:
• Dim <VariableName> [As DataType]
• A variable’s data type determines the kind of information a variable holds, such as
String, Integer, or Object.

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 62
Methods of Declaring Variables
• Implicit Declaration
• You don’t have to declare variables as a specific data type before you use them.
• If you use a variable without explicitly declaring it, Visual Basic creates a variable with
that name and assigns it the Variant data type.
• This is the largest data type available in terms of memory usage and could lead to
memory resource problems.

R. U. Kalyankar
• When you use an undeclared variable, accidentally misspelling the variable name creates
another undeclared variable.
• These kinds of errors can lead to poor performance and a lot of wasted time debugging
your code.
• Explicit Declaration
• To avoid the hazards mentioned, force Visual Basic to require that you declare all
variables before you use them.
• To do so, place the following line at the top of your code module:
Option Explicit

8/14/2021 SGGSIE&T, Nanded CCCS 63


Data Types
• VBA provides a number of data types. Each data type has a specific purpose that you
should consider when you choose which one to use.

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 64
User Interaction and the Utility Object
• To program truly powerful AutoCAD applications and macros, you need to be able
to interact with the user.
• This chapter discusses the Utility object
• which contains a number of methods for obtaining user input via the AutoCAD
command line and graphics screen

R. U. Kalyankar
• such as picking points, entering distances and angles, and picking a single entity.
• Interface Methods
• Interface methods present and retrieve information from the user.
• This section discusses the various methods made available through AutoCAD’s
Utility object.

8/14/2021 SGGSIE&T, Nanded CCCS 65


Input Methods and Dialogs
• All the AutoCAD input methods require that the user interact with the AutoCAD drawing
or command window.
• To use these methods from a VBA UserForm, either hide (Ex. Arc.Hide) the form before
you call the input methods or set the UserForm to modeless operation. (to show userform
Ex. Arc.Show)
• If you don’t, an error will occur.

R. U. Kalyankar
• This is because modal forms maintain the application focus while they’re visible.
• As long as a modal VBA form is visible, there’s no way for the user to get to the
AutoCAD drawing or command window.
• UserForms can be modal or modeless only with AutoCAD 2002 and later versions.
• To set the UserForm to modeless operation --->

8/14/2021 SGGSIE&T, Nanded CCCS 66


The Prompt Method
• The Prompt method displays messages on the AutoCAD command line. It returns
nothing.
• UtilityObject.Prompt Message
• The Message parameter is a string that contains the message to display at the
command prompt.

R. U. Kalyankar
• Ex.:
Public Sub TestPrompt()
ThisDrawing.Utility.Prompt vbCrLf & "This is a simple message"
End Sub

■Note Remember to include carriage returns or linefeeds in the message string, or


your message will just be appended to whatever text is on the command prompt.

8/14/2021 SGGSIE&T, Nanded CCCS 67


The InitializeUserInputMethod
• The InitializeUserInput method establishes which keywords the program will
accept during command line input.
• It also sets input criteria such as whether null values are allowed.
• It affects the very next call to an input function, after which the keywords and
other input criteria are cleared.

R. U. Kalyankar
• The InitializeUserInput method has this syntax:
UtilityObject.InitializeUserInput OptionBits[, KeywordList]

8/14/2021 SGGSIE&T, Nanded CCCS 68


The InitializeUserInputMethod (Cont...)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 69
The InitializeUserInputMethod (Cont...)
• Keywords:
• Command line keywords give the user various options. For instance, if you use the
AutoCAD
• Draw toolbar’s rectangle command, you may either pick a point or use one of the
five keywords to choose another command:

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 70
The Get Methods
• The Get methods get specific types of data from the user.
• These methods make AutoCAD pause until the user supplies a value at the
command prompt or picks a point in the drawing window.
• If the user supplies the wrong type of data, such as typing a string when a number
is needed, AutoCAD displays a message at the command prompt that tells them to
reenter the data.

R. U. Kalyankar
1. The GetKeyword Method:
• The GetKeyword method gets a command line option from the user.
• You must call InitializeUserInput to establish the list of keywords before you use
this method.
• It returns the keyword the user entered exactly as the keyword list specified it.
• This method has the following syntax:
strUserKeyWordInput = Object.GetKeyword([Prompt])

8/14/2021 SGGSIE&T, Nanded CCCS 71


The Get Methods (Cont.....)
• Ex.:
Sub TestGetKeyword()
Dim strInput As String
With ThisDrawing.Utility
.InitializeUserInput 0, "Line Arc Circle"
strInput = .GetKeyword(vbCr & "Command [Line/Arc/Circle]: ")

R. U. Kalyankar
End With
Select Case strInput
Case "Line": ThisDrawing.SendCommand "_Line" & vbCr
Case "Arc": ThisDrawing.SendCommand "_Arc" & vbCr
Case "Circle": ThisDrawing.SendCommand "_Circle" & vbCr
Case Else: MsgBox "You pressed Enter."
End Select
End Sub

8/14/2021 SGGSIE&T, Nanded CCCS 72


The GetXXX Methods (Cont.....)
2. The GetString Method:
The GetString method gets string values from the user. AutoCAD pauses until the
user enters a value.
This method has the following syntax:
dblUserStringInput = UtilityObject.GetString(HasSpaces[ ,Prompt])

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 73
The GetXXX Methods (Cont.....)
• This example gets a string from the user, including spaces, and then displays it:
Public Sub TestGetString()
Dim strInput As String
With ThisDrawing.Utility
strInput = .GetString(True, vbCr & "Enter a string: ")
.Prompt vbCr & "You entered '" & strInput & “’ “

R. U. Kalyankar
End With
End Sub

8/14/2021 SGGSIE&T, Nanded CCCS 74


The GetXXX Methods (Cont.....)
3. The GetInteger Method
• The GetInteger method gets an integer from the user. AutoCAD waits for the user to
input an integer and returns the entered value. This method has the following syntax:
• intUserIntegerInput = UtilityObject.GetInteger([Prompt])
• This method has one parameter, Prompt, a string. Optionally use it to specify a
prompt for input.

R. U. Kalyankar
• Ex.:
Public Sub TestGetInteger()
Dim intInput As Integer
With ThisDrawing.Utility
intInput = .GetInteger(vbCr & "Enter an integer: ")
.Prompt vbCr & "You entered " & intInput
End With
End Sub

8/14/2021 SGGSIE&T, Nanded CCCS 75


The GetXXX Methods (Cont.....)
4. The GetReal Method
• The GetReal method is similar to GetInteger but gets floating-point numbers.
• It returns a value of data type Double
• This method has the following syntax:
• dblUserRealInput = UtilityObject.GetReal([Prompt])

R. U. Kalyankar
• This method’s Prompt parameter, a string, is optional. Use it to specify a prompt for input.
• Ex.:
Public Sub TestGetReal()
Dim dblInput As Double
With ThisDrawing.Utility
dblInput = .GetReal(vbCrLf & "Enter an real: ")
.Prompt vbCr & "You entered " & dblInput
End With
End Sub

8/14/2021 SGGSIE&T, Nanded CCCS 76


The GetXXX Methods (Cont.....)
5. The GetPoint Method
• The GetPoint method gets a point from the user, either by typing coordinates at the
command prompt or by picking points in the drawing area.
• The return value is a Variant data type and contains a three-element array of doubles
holding the point’s World Coordinate System (WCS) coordinates.
• This method has the following syntax:

R. U. Kalyankar
• varUserPointInput = UtilityObject.GetPoint([BasePoint] [,Prompt])

• This method’s Prompt parameter, a string, is optional. Use it to specify a prompt for
input.
8/14/2021 SGGSIE&T, Nanded CCCS 77
The GetXXX Methods (Cont.....)
5. The GetPoint Method (Cont…)
• This example gets a point from the user and displays its coordinate values:
• Ex.:
Public Sub TestGetPoint()
Dim varPick As Variant

R. U. Kalyankar
With ThisDrawing.Utility
varPick = .GetPoint(, vbCr & "Pick a point: ")
.Prompt vbCr & varPick(0) & "," & varPick(1)
End With
End Sub
• This method raises exceptions for null input, keyword entry, and canceled input

8/14/2021 SGGSIE&T, Nanded CCCS 78


The GetXXX Methods (Cont.....)
6. The GetCorner Method
• Given a base point in a rectangle, the GetCorner method gets the diagonally opposing
corner point.
• It returns a Variant data type and contains a three-element array of doubles showing
the corner point’s WCS coordinates.

R. U. Kalyankar
• This method has the following syntax:
• varUserCornerInput = UtilityObject.GetCorner(BasePoint [,Prompt])

8/14/2021 SGGSIE&T, Nanded CCCS 79


The GetXXX Methods (Cont.....)
6. The GetCorner Method (Cont….)
• This example gets a point and then a corner from the user and displays the rectangle’s
values:
• Ex.:
Public Sub TestGetCorner()

R. U. Kalyankar
Dim varBase As Variant
Dim varPick As Variant
With ThisDrawing.Utility
varBase = .GetPoint(, vbCr & "Pick the first corner: ")
.Prompt vbCrLf & varBase(0) & "," & varBase(1)
varPick = .GetCorner(varBase, vbLf & "Pick the second: ")
.Prompt vbCr & varPick(0) & "," & varPick(1)
End With
End Sub

8/14/2021 SGGSIE&T, Nanded CCCS 80


The GetXXX Methods (Cont.....)
7. The GetDistance Method
• The GetDistance method gets a double from the user.
• It differs from GetReal in that the user can either type a distance in the current units
format or pick the point(s) on the graphics screen.
• These two methods are otherwise similar, and most people prefer GetDistance

R. U. Kalyankar
because it’s more flexible.
• This method has the following syntax:
• dblUserDistanceInput = UtilityObject.GetDistance([BasePoint] [,Prompt])

8/14/2021 SGGSIE&T, Nanded CCCS 81


The GetXXX Methods (Cont.....)
7. The GetDistance Method (Cont….)
• This example code sets the base point to the origin of WCS, prompts the user for
input, and then displays the value:
• Ex.:
Public Sub TestGetDistance()

R. U. Kalyankar
Dim dblInput As Double
Dim dblBase(2) As Double
dblBase(0) = 0: dblBase(1) = 0: dblBase(2) = 0
With ThisDrawing.Utility
dblInput = .GetDistance(dblBase, vbCr & "Enter a distance: ")
.Prompt vbCr & "You entered " & dblInput
End With
End Sub

8/14/2021 SGGSIE&T, Nanded CCCS 82


The GetXXX Methods (Cont.....)
8. The GetAngle Method
• Use the GetAngle to get an angle, in radians, from the user.
• The user may either type the angle at the command prompt or pick point(s) on the
screen.
• VBA ignores the points’ Z-coordinates.

R. U. Kalyankar
• It measures the angle counterclockwise with respect to the ANGBASE system
variable’s current value.
• This method has the following syntax:
• dblUserAngleInput = UtilityObject.GetAngle([BasePoint] [,Prompt])

8/14/2021 SGGSIE&T, Nanded CCCS 83


The GetXXX Methods (Cont.....)
8. The GetAngle Method (Cont….)
• This method returns the angle in radians regardless of the current setting of the
DIMAUNIT angular units system variable or the angular unit type the user entered.
• This example sets the angular units to degrees and then retrieves and displays an
angle from the user:

R. U. Kalyankar
• Ex.:
Public Sub TestGetAngle()
Dim dblInput As Double
ThisDrawing.SetVariable "DIMAUNIT", acDegrees
With ThisDrawing.Utility
dblInput = .GetAngle(, vbCr & "Enter an angle: ")
.Prompt vbCr & "Angle in radians: " & dblInput
End With
End Sub

8/14/2021 SGGSIE&T, Nanded CCCS 84


Creating 2D Object
• Add methods apply equally to the PaperSpace, ModelSpace, and Block objects.
• Therefore, when we present the syntax of each method in this section, the word
Object denotes any one of these collections.
• For example:
• Set CircleObject = Object.AddCircle(CenterPoint, Radius)

R. U. Kalyankar
• Each of the Add methods returns an object reference to the newly created entity, so
you must use the VBA Set operator if you want to assign this return to a variable.
• When you create or modify an entity, the changes to the drawing don’t display
until the Update method of that object, the Update method of the Application
object, or the Regen method of the Document object is called.
• In some cases, AutoCAD will update the display once your macro or program is
complete; however, it’s always safest to ensure the update takes place in your
code.

8/14/2021 SGGSIE&T, Nanded CCCS 85


Methods of Creating 2D Object
1. AddArc
2. AddCircle
3. AddEllipse
4. AddLine

R. U. Kalyankar
5. AddPoint

8/14/2021 SGGSIE&T, Nanded CCCS 86


Circular Objects
• The Arc Object
• You create an Arc object by using the AddArc method.
• This method takes four arguments that determine the position and size of the arc:
• Set ArcObject = Object.AddArc(CenterPoint, Radius, StartAngle, EndAngle)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 87
The Circle Object
• You can create a Circle object by specifying the position of the center and the
radius, and using the AddCircle method:
• Set CircleObject = Object.AddCircle(CenterPoint, Radius)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 88
The Ellipse Object
• You use the AddEllipse method to create a fully closed Ellipse object. This method
takes three parameters:
• Set EllipseObject = Object.AddEllipse(CenterPoint, MajorAxis, RadiusRatio)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 89
Line Objects
• Demonstrates how to create the various line-shaped objects.
• The Line Object
• The LWPolyline Object
• The MLine Object

R. U. Kalyankar
The Polyline Object
• Polyline Arc Segments
• The Ray Object
• The Spline Object
• The XLine Object

8/14/2021 SGGSIE&T, Nanded CCCS 90


The Line Object
• The AddLine method creates a Line object, which is a single, straight line running
between two points:
• Set LineObject = Object.AddLine(StartPoint, EndPoint)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 91
The LWPolyline Object
• A lightweight polyline is a 2-D line consisting of straight and arced segments.
• It’s functionally similar to the legacy Polyline entity, but it’s internally represented
in a “flat” data structure as opposed to the tree-structured Polyline entity.
• This results in a more compact data size per entity and faster graphics
regeneration; hence the “lightweight” name.

R. U. Kalyankar
• You create a new LWPolyline object using the AddLightWeightPolyline method,
as follows:
• Set LWPolylineObject = Object.AddLightWeightPolyline(Vertices)

8/14/2021 SGGSIE&T, Nanded CCCS 92


The LWPolyline Object (Cont…)
• Note
• Lightweight polylines don’t store Z-axis or elevation information with each vertex
point.
• Instead, the Polyline object has an Elevation property that specifies the Z elevation
relative to the coordinate system for all vertices.

R. U. Kalyankar
• When you create an LWPolyline object, it’s open as shown in Figure.
• To close the polyline (i.e., to join the first vertex to the last), set its Closed
property to True.

8/14/2021 SGGSIE&T, Nanded CCCS 93


The MLine Object
• The MLine object is a single graphic entity that consists of multiple parallel
straight-line segments.
• The maximum number of parallel lines is 16.
• You use the AddMLine method to create a new MLine object:
• Set MLineObject = Object.AddMLine(Vertices)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 94
The Polyline Object
• The Polyline object is similar in function to the LWPolyline object, but it’s stored
in an alternate format.
• It’s a 2-D or 3-D line consisting of straight and arced segments, but the segments
are stored as distinct entities.
• A number of different polyline types are available;

R. U. Kalyankar
• the following list illustrates their AutoCAD command-line equivalent and a
description of each type:
• PLINE = 2D polyline (AcadPolyline)
• 3DPOLY = 3D polyline (Acad3DPolyline)
• 3DMESH (also EDGESURF, RULESURF, TABSURF, etc.) = N x M 3D mesh
(AcadPolygonMesh)
• PFACE = Irregular 3D mesh (AcadPolyfaceMesh)

8/14/2021 SGGSIE&T, Nanded CCCS 95


The Polyline Object (Cont….)
• Each segment in an AcadPolyline object has a start point, an end point, and several
other unique properties.
• This object is less efficient than the lightweight polyline object, in which segments
are defined as a collection of vertex points.
• Use the AddPolyline method to create a Polyline defined by a set of vertices, as

R. U. Kalyankar
follows:
• Set PolylineObject = Object.AddPolyline(Vertices)

8/14/2021 SGGSIE&T, Nanded CCCS 96


The Polyline Object (Cont….)
• AutoCAD is set to draw lightweight polylines by default because of their
efficiency; you may change this default setting using the PLINETYPE system
variable.
• You can convert existing polylines from lightweight to heavy and vice versa by
using the Convertpoly command.

R. U. Kalyankar
• Although it’s less efficient for simple polyline representations, the Polyline object
has features not present in the lightweight form, such as the capability to represent
splines and other smoothly curved shapes.
• The following example allows the user to change a specified Polyline curve type
from the command line.

8/14/2021 SGGSIE&T, Nanded CCCS 97


Polyline Arc Segments
• When you first create them, both the LWPolyline and Polyline objects consist of
only straightline segments.
• Each polyline segment has an arc bulge factor that determines the segment’s arc
radius.
• By default, all segments have a bulge factor of 0, which is a straight line.

R. U. Kalyankar
• A positive bulge factor denotes a counterclockwise arc relative to the start and end
points of the polyline segment, and a negative bulge factor denotes a clockwise
arc.

8/14/2021 SGGSIE&T, Nanded CCCS 98


Polyline Arc Segments (Cont….)
• You derive the bulge factor from the arc radius by calculating the tangent of one-
fourth of the included angle between the polyline segment’s start and end points,
as shown in Figure.
• Because AutoCAD is already storing the segment points, a bulge factor is a very
efficient way to capture the arc radius.

R. U. Kalyankar
• Further, because the bulge factor is calculated using the segment’s start and end
points, the resulting arc is automatically scaled as the distance between these
points changes (e.g., when the user scales or grip-edits the polyline).
• To assign a bulge factor to the polyline segments, use the SetBulge method.

8/14/2021 SGGSIE&T, Nanded CCCS 99


The Ray Object
• The Ray object represents a line that extends to infinity in one direction from a
specified start point.
• The AddRay method uses the following syntax to create a new ray:
• Set RayObject = Object.AddRay(StartPoint, SecondPoint)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 100
The XLine Object
• The XLine object represents a straight line that passes through two specified
points and extends to infinity in both directions.
• You can use the AddXline method to create a new XLine object:
• Set XLineObject = Object.AddXline(FirstPoint, SecondPoint)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 101
The Spline Object
• The Spline object represents a nonuniform rational B-spline (NURBS) quadratic
or cubic curve.
• You use the AddSpline method to create a new Spline object:
• Set SplineObject = Object.AddSpline(FitPoints, StartTangent, EndTangent)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 102
The Region Object
• The Region object represents a bounded plane and may consist of Line, Arc,
Circle, Elliptical Arc, LightweightPolyline, and Spline objects.
• To create a Region object from a set of drawing entities, use the AddRegion
method as follows:
• RegionArray = Object.AddRegion(ObjectsArray)

R. U. Kalyankar
• AddRegion returns an array of the Region objects created from the ObjectsArray
parameter, as shown in figure.

8/14/2021 SGGSIE&T, Nanded CCCS 103


The Hatch Object
• The Hatch object represents an area filled with a pattern.
• The pattern may be associated with the area boundary (i.e., if the boundary
changes, the pattern also changes appropriately), or the pattern may be
independent of the area boundary.
• The read-only AssociativeHatch property determines whether the pattern is

R. U. Kalyankar
associated with the Hatch object.
• You set this property when you create the Hatch object, using the AddHatch
method.

8/14/2021 SGGSIE&T, Nanded CCCS 104


The Hatch Object (Cont…)
• Syntax for the AddHatch method:
• Set HatchObject = Object.AddHatch(PatternType, PatternName, Associativity)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 105
The Point Object
• Point objects can act as nodes to which you can snap objects. You can specify a
full 3-D WCS location for a point.
• You use the AddPoint method to create a new Point object, as follows:
• Set PointObject = Object.AddPoint(PointPosition)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 106
Creating 3-D Objects
• AutoCAD also provides you with a number of methods to programmatically
create complex 3-D solid entities.
• 3-D primitives (the box, cone, cylinder, sphere, torus, and wedge objects)
• Elliptical cones and cylinders
• Extruded and revolved solids

R. U. Kalyankar
• Manipulation of solids (slicing, checking interference, and spatial properties)

8/14/2021 SGGSIE&T, Nanded CCCS 107


Creating 3-D Objects
• The Box
• A box is a 3DSolid object with edges parallel to the axes of the WCS.
• You create it using the AddBox method:
• Set 3DSolidObject = Object.AddBox(BoxCenter, Length, Width, Height)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 108
The Cone
• You create a 3DSolid object in the form of a cone using the AddCone method.
• The base of the cone is parallel with the XY plane of the WCS.
• Set 3DSolidObject = Object.AddCone(ConeCenter, BaseRadius, Height)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 109
The Cylinder
• You add a cylinder to a drawing using the AddCylinder method.
• The base of the cylinder lies in a plane parallel to the WCS XY plane.
Set 3DSolidObject = Object.AddCylinder(CylinderCenter, Radius, Height)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 110
The Sphere
• You use the AddSphere method to create a 3DSolid object representing a sphere
and to add it to a drawing.
Set 3DSolidObject = Object.AddSphere(SphereCenter, Radius)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 111
The Torus
• Use the AddTorus method to create a torus and add it at a given point in a
drawing, such that the ring lays flat in the XY plane.
Set 3DSolidObject = Object.AddTorus(TorusCenter, TorusRadius, TubeRadius)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 112
The Wedge
• The AddWedge method creates a wedge with edges parallel to the WCS axes.
• Set 3DSolidObject = Object.AddWedge(FaceCenter, Length, Width, Height)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 113
Creating Elliptical 3-D Objects
• AutoCAD provides two methods for creating elliptical solids.
• The first creates an elliptical cone, and the second creates an elliptical cylinder.
The Elliptical Cone
• The AddEllipticalCone method creates a 3DSolid object in the form of an
elliptical cone whose base lies flat on the WCS XY plane.

R. U. Kalyankar
• The major axis of the ellipse may lie in either the X direction or the Y direction.
• Set 3DSolidObject = Object.AddEllipticalCone(ConeCenter, XLength, YLength,_
Height)

8/14/2021 SGGSIE&T, Nanded CCCS 114


Creating Elliptical 3-D Objects (Cont…)
The Elliptical Cylinder
• Use the AddEllipticalCylinder method to add a 3DSolid elliptical cylinder whose
base lies parallel to the WCS XY plane.
• The major axis of the elliptical base lies either in the X direction or in the Y
direction.

R. U. Kalyankar
Set 3DSolidObject = Object.AddEllipticalCylinder (CylinderCenter,_
XLength, YLength, Height)

8/14/2021 SGGSIE&T, Nanded CCCS 115


Creating Extruded and Revolved Objects
• You create extruded and revolved solids by taking planar Region objects and
adding thickness to them, either by extruding them along the Z-axis or by
revolving them around an arbitrary axis.
• The newly created entities are 3DSolid objects.

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 116
The Extruded Solid
• You create this solid by extruding a planar region along its Z-axis.
• You may extrude the region in a positive or negative direction, and you may taper
or expand it in the extrusion direction.
Set 3DSolidObject = Object.AddExtrudedSolid(Region, Height, TaperAngle)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 117
The Extruded Solid Along a Path
• You use the AddExtrudedSolidAlongPath method to create a new 3DSolid object
that represents the extrusion of a closed planar region along a given path.
• This path may take the form of a polyline, spline, circle, ellipse, or arc.
• The new solid is extruded from the current location of the region using the
translation of the path to the region’s Centroid.
• Set 3DSolidObject = Object.AddExtrudedSolidAlongPath(Region, Path)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 118
The Revolved Solid
• You add a 3DSolid object representing a revolved solid, which you create by
sweeping a planar region around an axis, to a drawing by using the
AddRevolvedSolid method.
Set 3DSolidObject = Object.AddRevolvedSolid(Region, AxisPoint, _
AxisDirection, Angle)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 119
Editing Solids
• You may combine and edit individual 3DSolid objects to form new, more complex
3DSolid objects.
• You may perform operations to combine solids, subtract one solid from another,
and find the common intersection between solids.

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 120
Boolean Operations
• You perform the primary editing operations using the Boolean method.
• This method, whose name is derived from its algebraic nature, alters the solid
according to the Operation parameter.
3DSolidObject.Boolean(Operation, SolidObject)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 121
Editing Objects
• AutoCAD gives you plenty of methods and properties so you can edit drawing
objects, which you’d normally have to do in the AutoCAD interface.
• By employing these methods and properties, you can adapt and combine editing
tasks in macros and VBA applications as needed.
• This topic considers editing objects, first through their methods and then through

R. U. Kalyankar
their properties.
• Specifically, this chapter covers the following:
➢ Copying, deleting, exploding, highlighting, mirroring, moving, offsetting,
rotating, and scaling objects
➢ Working with polar and rectangular arrays
➢ Changing the color, layer, linetype, and visibility of objects

8/14/2021 SGGSIE&T, Nanded CCCS 122


Editing with Methods
• AutoCAD VBA editing methods edit differently from properties.
• Methods generally change an entity’s shape, size, and position or create a new
entity based on the original.
• Properties, on the other hand, tend to change the appearance of the object
boundary lines, which represent the objects on the screen or on the plotter.

R. U. Kalyankar
• For each object against which you want to invoke a method, be sure that the object
supports the method you are using.
• Your best tools for this are error trapping and error handling.

8/14/2021 SGGSIE&T, Nanded CCCS 123


Copying Objects
• Use the Copy method to copy an existing drawing object.
• The new object occupies the same position as the original object and is drawn on
top of all other objects.
• This method has the following syntax:
• Set DrawingObject = DrawingObject.Copy

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 124
Deleting Objects
• To remove an object from a drawing, use the Delete method, which has the
following simple syntax:
• Object.Delete

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 125
Exploding Objects
• Use the Explode method to break a compound object into its subentities.
• It returns an array of objects that hold references to these subentities.
• This might mean breaking a block reference into its constituent drawing objects or a
polyline into its individual straight-line polylines.
• Block Insertions leave the original block object behind, and the resulting exploded set of
entities is actually a copy of each original nested entity in the Block Insertion.

R. U. Kalyankar
• To produce behavior similar to the AutoCAD EXPLODE command, also delete the
original Block Insertion entity.
• This method has the following syntax:
• varObjectArray = Object.Explode
• The Erase method is similar to the Delete method, except it erases only SelectionSet
groups. You can’t use it to delete objects.
• The 3DPoly, BlockRef, LightweightPolyline, MInsertBlock, PolygonMesh, Polyline,
Xref, and Region objects expose this method.

8/14/2021 SGGSIE&T, Nanded CCCS 126


Highlighting Entities
• Use the Highlight method to set whether an entity is highlighted in the drawing editor
window.
• Changes to an entity’s highlight become effective only when you update or regenerate the
entity.
• You can apply this method to drawing entities and the Group and SelectionSet objects.

R. U. Kalyankar
• This method has the following syntax:
• Object.Highlight Highlighted
• This method’s Highlighted parameter, a Boolean, indicates whether to highlight the object
• (True) or not (False).
• The following example illustrates the Highlight method in action.
• It asks the user to choose a selection of objects from the drawing area.
• It then highlights and unhighlights each of these objects.

8/14/2021 SGGSIE&T, Nanded CCCS 127


Mirroring Objects
• Use the Mirror method to create an object that is the mirror image of an existing
planar object.
• This method has the following syntax:
• Set DrawingObject = DrawingObject.Mirror(Point1, Point2)

R. U. Kalyankar
• Note that the MIRRTEXT system variable has been set to 0.
• This setting mirrors text objects tothe appropriate place on the drawing, but does
not mirror the text itself, as shown in Figure
8/14/2021 SGGSIE&T, Nanded CCCS 128
Mirroring Objects (Cont…)
• Note that the MIRRTEXT system variable has been set to 0.
• This setting mirrors text objects to the appropriate place on the drawing, but does
not mirror the text itself, as shown in Figure

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 129
Mirroring Objects (Cont…)
• Use the Mirror3D method to reflect an object in a plane, where the new object lies
outside the plane of the original object.
• Set DrawingObject = DrawingObject.Mirror3D(PlanePoint1, PlanePoint2,
PlanePoint3)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 130
Moving Objects
• Use the Move method to perform three-dimensional translations on drawing
objects.
• Thismethod has the following syntax:
• DrawingObject.Move Point1, Point2

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 131
Rotating Objects
• Use the Rotate method to rotate a drawing object around a given point in the User
Coordinate System’s (UCS) XY plane.
• This method has the following syntax:
• DrawingObject.Rotate BasePoint, RotationAngle

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 132
Rotating Objects (Cont…)
• Use the Rotate3D method to perform a three-dimensional rotation of an object
around an axis.
• This method has the following syntax:
• DrawingObject.Rotate3D AxisPoint1, AxisPoint2, RotationAngle

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 133
Scaling Objects
• The ScaleEntity method scales a drawing object uniformly in all directions around
a base point.
• This method has the following syntax:
• DrawingObject.ScaleEntity BasePoint, ScaleFactor

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 134
Offsetting Objects
• The Offset method creates a new object with boundaries offset a specified distance
from an existing object’s boundaries.
• You can apply this method to the Arc, Circle, Ellipse, Line, LightweightPolyline,
Polyline, Spline, and Xline objects.
• This method returns an array of the newly created object(s).

R. U. Kalyankar
• varObjectArray = Object.Offset(OffsetDistance)

8/14/2021 SGGSIE&T, Nanded CCCS 135


Object Arrays
• Use the ArrayPolar and ArrayRectangular methods to create an array of objects
based on an existing object.
• Both methods copy the base object into a regular pattern at a specified distance
from one another.

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 136
Creating a Polar Array of Objects
• A polar array is a pattern of drawing objects lying on an arc’s path.
• The ArrayPolar method creates a polar array, placing a specified number of objects
over an angle.
• It returns an array of the new objects.
• varObjectArray = DrawingObject.ArrayPolar (NumberofObjects, _

R. U. Kalyankar
AngleToFill, ArrayCenter)

8/14/2021 SGGSIE&T, Nanded CCCS 137


Creating a Rectangular Array of Objects
• A rectangular array is a grid pattern of drawing objects.
• This pattern may be two or three dimensional and you can construct it using the
ArrayRectangular method.
• Just like the ArrayPolar method, this method returns an array of objects that hold a
reference to the new drawing objects.

R. U. Kalyankar
• varObjectArray = DrawingObject.ArrayRectangular (NumberOfRows, _
NumberOfColumns, NumberOfLevels, DistBetweenRows, _
DistBetweenColumns, DistBetweenLevels)

8/14/2021 SGGSIE&T, Nanded CCCS 138


Creating a Rectangular Array of Objects
• varObjectArray = DrawingObject.ArrayRectangular (NumberOfRows, _
NumberOfColumns, NumberOfLevels, DistBetweenRows, _
DistBetweenColumns, DistBetweenLevels)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 139
Creating Linetypes
After completing this topic, you will be able to:
• Write linetype definitions.
• Create different linetypes.
• Create linetype files.

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 140
Creating Linetypes (Cont….)
Standard Linetypes:
• The AutoCAD software package comes with a library of standard linetypes that
has different linetypes, including ISO linetypes.
• These linetypes are saved in the acadiso.lin file.
• You can modify existing linetypes or create new ones.

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 141
Creating Linetypes (Cont….)
Linetype Definition:
• Consist of two parts:
Header line and pattern line
Header Line:
• The format of the header line is:

R. U. Kalyankar
*Linetype_Name, Description
• Ex.
*HIDDENS, - - - - - - - - - - - - - - - -
• Pattern Line:
• The format of the pattern line is:
Alignment field specification, Linetype specification
• Ex. A,.75,-.25,.75

8/14/2021 SGGSIE&T, Nanded CCCS 142


Creating Linetypes (Cont….)
• About Text in Custom Linetypes

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 143

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 144
Control Structures
• Control your application’s flow and execution.
• Without control structures, your application would run from top to bottom.
• Decision Structures
• Decision structures let you test the condition, value, or state of a variable and perform
operations based on the test’s result.

R. U. Kalyankar
• If ... Then
• Use an If ... Then statement to conditionally execute one or more other statements. The
• If ... Then statement is used either in this way:
• If <condition> Then <statement>
• or in this way:
• If <condition> Then
• <statements>
• End If

8/14/2021 SGGSIE&T, Nanded CCCS 145


Decision Structures
• The <condition> can be an expression that evaluates to a numeric value.
• Visual Basic interprets the numeric value as either True (nonzero) or False (zero).
• When the <condition> evaluates to True, then Visual Basic executes the
<statement(s)> following the Then keyword, as in the following single-line If ... Then
statement:

R. U. Kalyankar
If Count < 20 Then Count = Count + 1
• To execute more than one statement after the conditional test, write it like this:
If Count < 20 Then
Count = Count + 1
Application.Visible = False
End If
The multiple-line version of the If ... Then statement requires that you end the statement
• with the End If keywords.

8/14/2021 SGGSIE&T, Nanded CCCS 146


Decision Structures
• If ... Then ... Else
• To test several conditions, define several blocks of statements where a statement is
executed only when its condition is met. For example:
If <condition1> Then
<statement block 1>

R. U. Kalyankar
Else If
<statement block 2>
Else
<statement block 3>
End If

8/14/2021 SGGSIE&T, Nanded CCCS 147


Custom Tool Requirement Analysis related to CAD
• Design calculations.
• Design of any standard in the world can be quickly modelled (ISO, etc).
• Can be model various types
• Type1:
• Subtype1

R. U. Kalyankar
• Subtype2
• Type2:
• Subtype1
• Subtype2
• Data entry of parameters are possible.
• Readymade input (Data can be selected from connected standard
tables/spreadsheets).
• Automatic reports can be generated.
8/14/2021 SGGSIE&T, Nanded CCCS 148
Custom Tool Requirement Analysis related to CAD (Cont..)
• Bearings
a. Design calculations
b. Design of any standard in the world can be quickly modeled (ISO, DIN,
HASCO, etc)
c. Can be model following types of bearings,

R. U. Kalyankar
• Ball Bearings:
• Subtype1
• Subtype2
• Radial Bearings:
• Subtype1
• Subtype2

8/14/2021 SGGSIE&T, Nanded CCCS 149


R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 150
R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 151
R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 152
R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 153
R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 154
R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 155
R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 156
R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 157
R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 158
Dimensions and Annotations
• A typical user will add textual annotations and measurement annotations
(dimensions) to a drawing for manufacturing, modeling, engineering, mapping,
and surveying purposes and to help clarify design intent.
• AutoCAD VBA provides a number of methods to help users create annotations
and dimensions that they normally create through the AutoCAD application
interface.

R. U. Kalyankar
• In this topic you’ll learn how to create dimensions and annotations and add them
to your drawings.
• You’ll also see how to set up and use different text and dimension styles.

8/14/2021 SGGSIE&T, Nanded CCCS 159


Working with Dimensions
• We can add dimension entities to drawings to show the dimension or size of
different drawing elements.
• We can use them to measure angles, distances, and chords.
• In this section, we will examine each of the seven-dimension objects in turn.
• First, however, we’ll look at the DimStyle object, which we use to determine the

R. U. Kalyankar
appearance of dimensions, leaders, and geometric tolerances.

8/14/2021 SGGSIE&T, Nanded CCCS 160


Working with Dimensions (Continued)
Using the DimStyle Object
• The DimStyle object represents some settings that determine the appearance of a
dimension, tolerance, or leader.
• DimStyle objects are held in the DimStyles collection, and we may access them via the
DimStyles collection’s Item method.
• To create a new DimStyle object through code, you need to use the Add method of the

R. U. Kalyankar
DimStyles collection.
Set DimStyleObject = DimStylesCollection.Add(DimStyleName)

• The following code shows how to add a new DimStyle object:


Dim objDimStyle As AcadDimStyle
Set objDimStyle = ThisDrawing.DimStyles.Add("NewDimStyle")

8/14/2021 SGGSIE&T, Nanded CCCS 161


Creating Dimensions
• You can create linear, radial, angular, and ordinate dimensions.
• When creating dimensions, the active dimension style is used.
• Once created, you can modify the extension line origins, the dimension text location, and the
dimension text content and its angle relative to the dimension line.
• You can also change the dimension style used by the dimension.
• Dimension objects are entities that provide information about distances and angles within an

R. U. Kalyankar
AutoCAD drawing.
• The AddDimXYZ methods used to add dimensions to drawings are exposed by the ModelSpace,
PaperSpace and Block objects.

8/14/2021 SGGSIE&T, Nanded CCCS 162


Creating Dimensions (Continued)
The DimAligned Object
• You use the DimAligned object to display the length of a line.
• Extension lines emanate at right angles from the ends of the line to be measured to
the level of the dimension text.
• You use the AddDimAligned method to add this object to your drawing.

R. U. Kalyankar
Set DimAlignedObject = Object.AddDimAligned(Point1, Point2, _
TextPosition)

8/14/2021 SGGSIE&T, Nanded CCCS 163


Creating Dimensions (Continued)
The Dim3PointAngular Object
• The Dim3PointAngular object displays the angular distance between three points.
• You can use the AddDim3PointAngular method to add this type of dimension to a
drawing.
Set Dim3PointAngularObject = Object.AddDim3PointAngular(VertexPoint,_

R. U. Kalyankar
Point1, Point2, TextPosition)

8/14/2021 SGGSIE&T, Nanded CCCS 164


Creating Dimensions (Continued)
The DimAngular Object
• The DimAngular object is much like the Dim3PointAngular object.
• It’s designed to hold dimension text displaying the angle between two lines or
spanned by an arc or circle.
• You use the AddDimAngular method to create a new DimAngular object.

R. U. Kalyankar
Set DimAngularObject = Object.AddDimAngular(Vertex, Point1, Point2,_
TextPosition)

8/14/2021 SGGSIE&T, Nanded CCCS 165


Creating Dimensions (Continued)
The DimDiametric Object
• The DimDiametric object represents a dimension showing the length of a chord
across a circle or arc.
• The AddDimDiametric method creates a new diametric dimension.
Set DimDiametricObject = Object.AddDimDiametric (ChordPoint1,_

R. U. Kalyankar
ChordPoint2, LeaderLength).

8/14/2021 SGGSIE&T, Nanded CCCS 166


Creating Dimensions (Continued)
The DimRadial Object
• A radial dimension displays the length of the radius of a circle or arc.
• You create it using the AddDimRadial method.
Set DimRadialObject = Object.AddDimRadial (CenterPoint, ChordPoint,_
LeaderLength).

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 167
Creating Dimensions (Continued)
The DimRotated Object
• The DimRotated object measures the length of the projection of a line onto a
direction specified by an angle from the X-axis.
Set DimRotatedObject = Object.AddDimRotated(Point1, Point2, _
DimLocationPoint, RotationAngle)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 168
Using the Tolerance Object
• The Tolerance object represents a geometric tolerance in a feature control frame.
• Tolerances specify allowable deviations of orientation, location, and so on, from
the exact geometry of a drawing.
Set ToleranceObject = Object.AddTolerance(Text, InsertionPoint, _
TextDirection)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 169
Using the Tolerance Object (Continued)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 170
Using the Tolerance Object (Continued)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 171
Using the Leader Object
• A Leader object consists of an arrowhead attached to a spline or to straight-line
segments.
• You can use this object to associate text with a drawing object.
• To add a Leader object to your drawing, use the AddLeader method.
Set LeaderObject = Object.AddLeader(PointsArray, Annotationtype,

R. U. Kalyankar
ArrowType).

8/14/2021 SGGSIE&T, Nanded CCCS 172


Controlling Layers and Linetypes
• Effective use of layers and linetypes is the key to creating structured drawings and
manipulating AutoCAD in an efficient manner.
• In this section, you’ll see how to access the Layers and Linetypes collections
and their respective Layer and Linetype objects and learn about their methods
and properties.

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 173
Layers
• A layer is a property of every AutoCAD drawing object.
• By using multiple layers, you may organize drawing data into logical categories.
• For example, when you design an office layout, you could use one layer to
display the walls and other fixed structural objects, and you could use other
layers to show the potential furniture or electrical arrangements.

R. U. Kalyankar
• Alternatively, you could use a layer to hold dimensions (measurement
annotations) or hidden lines.
• A Layer object represents one of these logical groupings.
• Manipulating the state of layers makes it easier to manage complex drawings.
• For example, by making a layer visible or hidden, the user can choose to work
with specific entity categories without being overwhelmed by all the other
drawing entities.

8/14/2021 SGGSIE&T, Nanded CCCS 174


Accessing Layers
• AutoCAD has a Layers collection that contains all the Layer objects in the
drawing.
• You can create as many layers as you want by adding new Layer objects to the
Layers collection.
• You access the Layers collection via a Document object.

R. U. Kalyankar
• In the following code, ThisDrawing is used as the active document:
Dim objLayers As AcadLayers
Set objLayers = ThisDrawing.Layers

8/14/2021 SGGSIE&T, Nanded CCCS 175


Checking for Existing Layers
Public Sub CheckForLayerByIteration()
Dim objLayer As AcadLayer
Dim strLayerName As String
strLayername = InputBox("Enter a Layer name to search for: ")
If "" = strLayername Then Exit Sub ' exit if no name entered

R. U. Kalyankar
For Each objLayer In ThisDrawing.Layers ' iterate layers
If 0 = StrComp(objLayer.name, strLayername, vbTextCompare) Then
MsgBox "Layer '" & strLayername & "' exists"
Exit Sub ' exit after finding layer
End If
Next objLayer
MsgBox "Layer '" & strLayername & "' does not exist"
End Sub

8/14/2021 SGGSIE&T, Nanded CCCS 176


Creating a New Layer
• You can create as many layers as needed.
• The Add method is used to create a Layer object and to add it to the Layers
collection.
Set LayerObject = LayerCollection.Add(LayerName)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 177
The following example retrieves a layer name from the user and
attempts to add it to the Layers collection:
Public Sub AddLayer()
Dim strLayerName As String
Dim objLayer As AcadLayer
strLayerName = InputBox("Name of Layer to add: ")
If "" = strLayerName Then Exit Sub ' exit if no name entered
On Error Resume Next ' handle exceptions inline
'check to see if layer already exists

R. U. Kalyankar
Set objLayer = ThisDrawing.Layers(strLayerName)
If objLayer Is Nothing Then
Set objLayer = ThisDrawing.Layers.Add(strLayerName)
If objLayer Is Nothing Then ' check if obj has been set
MsgBox "Unable to Add '" & strLayerName & "'"
Else
MsgBox "Added Layer '" & objLayer.Name & "'"
End If
Else
MsgBox "Layer already existed"
End If
End Sub

8/14/2021 SGGSIE&T, Nanded CCCS 178


Creating a New Layer (Cont...)
• When a layer is first created, its properties are set to certain default values.
• Figure shows the Layer Properties Manager dialog box.
• The first, layer 0, is created automatically by AutoCAD and may not be deleted or
renamed.
• The second is the layer that was just created through code. The default attribute

R. U. Kalyankar
settings are shown.

8/14/2021 SGGSIE&T, Nanded CCCS 179


Making a Layer Active
• When you create new entities in AutoCAD, they’re placed on the current or active layer.
• Therefore, to draw entities on a specific layer, you must first make that layer active.
• Note: You can’t make a layer active if it’s frozen.
• The ActiveLayer property is a member of the Document object.
• To make a specific layer active, assign the Layer object to the ActiveLayer.

R. U. Kalyankar
DocumentObject.ActiveLayer = LayerObject
• The following code makes the layer named “Walls” the active layer for the current
Document:
ThisDrawing.ActiveLayer = ThisDrawing.Layers("Walls")
• New entities will now be placed on the “Walls” layer until another layer is made active.
• You can change the layer of existing entities by altering the Layer property of that
particular entity.
• The Layer property is a string that corresponds to the name of a Layer object.

8/14/2021 SGGSIE&T, Nanded CCCS 180


Turning a Layer On/Off
• Turning a layer on or off allows you to control its visibility on the screen.
• You can also hide a layer by freezing it.
• This is more suitable if you want to hide a layer for a considerable length of time
• Turning a layer on/off is more suited to situations in which the visibility will be
changed frequently.

R. U. Kalyankar
LayerObject.LayerOn = blnLayerOn
• A value of True for this property will turn the layer on.
• Conversely, a value of False will turn the layer off.
• Setting the Freeze property to True will freeze a layer, whereas setting it to False
will thaw the layer.
• For example, the following freezes a specific Layer object:
objLayer.Freeze = True

8/14/2021 SGGSIE&T, Nanded CCCS 181


Locking/Unlocking a Layer
• The Lock property holds a Boolean value, and setting it to True will lock a layer.
• A value of False will unlock the layer, for example:
objLayer.Lock = False
• Making Layers Plottable
• You can control the ability to plot layers by manipulating the Plottable property of the

R. U. Kalyankar
Layer object.
• Setting it to True enables it to be processed for plotting.
• Setting it to False treats it as though it has been frozen with respect to plot output.
objlayer.Plottable = False
• Deleting a Layer
• The Layer.Delete method removes a Layer object from the Layers collection.
• This method takes no parameters and has no return value:
LayerObject.Delete

8/14/2021 SGGSIE&T, Nanded CCCS 182


Layer Colors
• Color property is set to ByLayer.
objLayer.Color = acRed
or
objLayer.Color = 1

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 183
Layer Linetypes
• The Layer.Linetype property is a string that you can access as follows:
Public Sub Layer0Linetype()
Dim objLayer As AcadLayer
Dim strLayerLinetype As String
Set objLayer = ThisDrawing.Layers("layer4")

R. U. Kalyankar
objLayer.Linetype = "Continuous"
strLayerLinetype = objLayer.Linetype
End Sub

8/14/2021 SGGSIE&T, Nanded CCCS 184


Loading a Linetype
• Linetype definitions are stored in linetype library files, which are external to
drawing files and have the extension .lin.
• Linetypes are loaded by name from library files into similarly named Linetype
objects in the drawing.
• Once loaded into a drawing, a Linetype object has no more connection to the

R. U. Kalyankar
library file.
• No methods exist for customizing linetypes programmatically
• Set LinetypeObject = LinetypesCollection.Load(LinetypeName, _
LinetypeFilename)

8/14/2021 SGGSIE&T, Nanded CCCS 185


Loading a Linetype Example
Public Sub LoadLinetype()
Dim strLinetypeName As String
Dim objLinetype As AcadLineType
strLinetypeName = InputBox("Enter a Linetype name" & _
" to load from ACAD.LIN: ")
If "" = strLinetypeName Then Exit Sub ' exit if no name entered

R. U. Kalyankar
On Error Resume Next ' handle exceptions inline
ThisDrawing.Linetypes.Load strLinetypeName, "acad.lin"
If Err Then ' check if err was thrown
MsgBox "Error loading '" & strLinetypeName & "'" & vbCr & _
Err.Description
Else
MsgBox "Loaded Linetype '" & strLinetypeName & "'"
End If
End Sub

8/14/2021 SGGSIE&T, Nanded CCCS 186


Sample Projects
• Splines

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 187
R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 188
UserForms
• The UserForm object is the canvas upon which you visually design your
application, and it provides the windows your users interact with when they run
the application.
• UserForms have their own properties, methods, and events you can use to
control their appearance and behavior.

R. U. Kalyankar
SGGSIE&T CCCS
Inserting a user Form:

R. U. Kalyankar
SGGSIE&T CCCS
ActiveX Controls:

R. U. Kalyankar
SGGSIE&T CCCS
AutoCAD UserForm Events:
• Events occur as a result of actions happening while your program is running, such
as opening or saving a drawing. They allow you to write source code that will
execute whenever that event occurs.
• Messages such as “Would you like to save changes?” are the common results of a
user action that has triggered an event.

R. U. Kalyankar
SGGSIE&T CCCS
List of Events

R. U. Kalyankar
SGGSIE&T CCCS
Click Event
Private Sub cmdPick_Click()
Dim Point As Variant
On Error Resume Next
'hide the UserForm
UserForm1.Hide
'ask user to select a point
Point = ThisDrawing.Utility.GetPoint(, "Select a point")

R. U. Kalyankar
If Err Then Exit Sub
'assign values to appropriate textbox
txtX = Point(0): txtY = Point(1): txtZ = Point(2)
'redisplay the UserForm
UserForm1.Show
End Sub

Private Sub cmdOK_Click()


Unload Me
End Sub

SGGSIE&T CCCS
Setting UserForm Properties

• UserForm properties control a form’s physical appearance.

• Changing width and height through programing:

R. U. Kalyankar
Ex. Private Sub UserForm_Activate()
UserForm1.Width = 200
UserForm1.Height = 150
End Sub

SGGSIE&T CCCS
Label Activate Event
Ex.:

Private Sub UserForm_Activate()


Label1.Caption = "Blocks"

R. U. Kalyankar
End Sub

SGGSIE&T CCCS
Key Up Event on Textbox

• The following example waits until the user presses the Enter
key, and then prints the text entered in a standard Visual Basic
MsgBox dialog box:
Ex.:

R. U. Kalyankar
Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)
If KeyCode = 13 Then
MsgBox "You entered: " & TextBox1.Text
End If
End Sub

SGGSIE&T CCCS
Combo Box Control
Ex.:

Private Sub UserForm_Activate()


With ComboBox1
.AddItem "Item 1"

R. U. Kalyankar
.AddItem "Item 2"
.AddItem "Item 3"
End With
End Sub

Private Sub ComboBox1_Click()


MsgBox "You choose: " & ComboBox1.List(ComboBox1.ListIndex)
End Sub

SGGSIE&T CCCS
Option Button
• Also known as Radio Buttons.
Ex.:
Private Sub OptionButton1_Click()
MsgBox "OptionButton1"
End Sub

R. U. Kalyankar
Private Sub OptionButton2_Click()
MsgBox "OptionButton2"
End Sub

SGGSIE&T CCCS
Toggle Button
Ex.:

Private Sub ToggleButton1_Click()


Select Case ToggleButton1.Value
Case False

R. U. Kalyankar
MsgBox "ToggleButton1 is Off"
Case True
MsgBox "ToggleButton1 is On"
End Select
End Sub

SGGSIE&T CCCS
Controlling Menus and Toolbars
• AutoCAD 2000 (and higher) gives you the ability to control the menus and
toolbars programmatically.
• You can manipulate existing menus or create new entries using the objects
exposed by the AutoCAD object model.
• Although you can’t create a completely new menu structure programmatically,

R. U. Kalyankar
you can make changes to the existing menus.
• I will start this topic with the MenuGroups collection because it’s the parent object
of the MenuGroup object, which in turn is the parent object of the ToolBars and
PopupMenus collections.
• The MenuBar collection holds all the PopupMenu objects that are currently
displayed in the AutoCAD menu bar.

8/14/2021 SGGSIE&T, Nanded CCCS 201


The MenuGroups Collection
• The menus loaded into the current session of AutoCAD are contained in the
MenuGroups collection.
• These menus, grouped into MenuGroup objects, may or may not be visible on the
menubar, but they’re all still contained within this collection.
• Each MenuGroup object provides access to the toolbars and pop-up menus

R. U. Kalyankar
available within an AutoCAD session.

8/14/2021 SGGSIE&T, Nanded CCCS 202


Loading Menu Groups
• You can use the Load method of the MenuGroups collection to load a new menu group
contained in a menu file (.mnc, .mns, .mnu, or .cui) into an AutoCAD session.
• Set MenuGroupObject = MenuGroupsCollection.Load (MenuFileName [,BaseMenu])

R. U. Kalyankar
• Using the Load method with the BaseMenu parameter set to True equates to executing the
MENU command inside the AutoCAD application and selecting a file through the Select
Customization File dialog box.
• Alternatively, you can execute the MENULOAD or CUILOAD command and check the
Replace All option in the Menu Customization dialog box.
• The newly loaded menu file becomes the only loaded menu group and completely
replaces the previous menu bar.

8/14/2021 SGGSIE&T, Nanded CCCS 203


Adding Menus to the Menu Bar
• You can add a new menu to the menu bar in two ways.
• The first uses the InsertInMenuBar method of the PopupMenu object to be
inserted.
• When a menu not displayed on the AutoCAD menu bar is added to the menu bar,
its OnMenuBar property changes from False to True.

R. U. Kalyankar
• PopupMenuObject.InsertInMenuBar(Index)

8/14/2021 SGGSIE&T, Nanded CCCS 204


Creating Tables
• AutoCAD has a new object aimed at creating drawing tables for bills of materials.
• The new object, Table, allows you to create a rectangular array of any number of
rows and columns and populate it with virtually any data you like.
• Let’s see basics of creating and manipulating tables for bills of materials.

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 205
The AddTable Method
• You can create a Table object by specifying the insertion point, number of rows,
number of columns, row height, and column width using the AddTable method.
• Set TableObject = Object.AddTable(InsertionPoint, NumberOfRows, _
NumberOfColumns, RowHeight, ColumnWidth)

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 206
The RegenerateTableSuppressed Property
• The RegenerateTableSuppressed property specifies whether the table will be
repainted each time a change to the table is made.
object.RegenerateTableSuppressed

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 207
The GetText Method
• Text within a Table can be read using the GetText method to retrieve the text string
value of a specified row and column of an existing table.
• The GetText method returns a string and has the following syntax:
object.GetText Row, Column

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 208
The SetText Method
• Use the SetText method to set the text string value of a specified row and column
of an existing table.
• The SetText method returns nothing but has the following syntax:
object.SetText Row, Column, Text

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 209
The GetTextHeight Method
• Use the GetTextHeight method to retrieve the row height of a specific type of row.
• The GetTextHeight method returns the height of the row type specified and has the
following syntax:
object.GetTextHeight RowType

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 210
The InsertRows Method
• Use the InsertRows method to insert additional rows in an existing table.
• Note: Rows are inserted before the row index provided as a parameter to this
method.
• The method returns nothing but has the following syntax:
object.InsertRows Index, RowHeight, Rows

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 211
The InsertColumns Method
• Use the InsertColumns method to insert additional columns in an existing table.
• Note: Columns are inserted before the column index provided as a parameter to
this method.
• The method returns nothing but has the following syntax:
• object.InsertColumns Index, ColumnWidth, Columns

R. U. Kalyankar
8/14/2021 SGGSIE&T, Nanded CCCS 212
R. U. Kalyankar
Thank You
8/14/2021 SGGSIE&T, Nanded CCCS 213

You might also like