You are on page 1of 40

CAD programming

CAD systems are powerful software


applications.
They employ complex geometric modeling
concepts, computer graphics algorithms,
software techniques, design and analysis
theories, manufacturing techniques, and
database and management concepts.
CAD software of any commercial system
has been evolving over the years. The
code itself is very long, complex, and
difficult to follow.
Modifying and programming CAD software
is a difficult, but not impossible, task.

We can, and do, modify CAD software to


meet our modeling needs. After all, it is
engineers who work for CAD vendors and
write the CAD software.
CAD software is written in a variety of
languages. Legacy systems, such as
Unigraphics and I-DEAS, started as
Fortran code. Newer systems, such as
Pro/E and SolidWorks, are written in C
and C++. ACIS is written in C++ and also
uses Scheme. Most of these systems use
Java also in one form or another,
especially for Web-based functionality.
How often do we need to program
CAD systems?
• In comparison to using the systems as
they are, the need for programming is not
great. This is not to say that customizing
these systems is hardly needed. There are
different levels of customization and
programming, ranging from writing a
simple macro to automate repetitive tasks
to using the system API (application
programming interface) functions to
extend the system functionality.
Users who are interested in programming
their CAD systems must be familiar with
system functionality, structure, hierarchy,
terminology, and philosophy. Such
familiarity is a precursor to being able to
use the programming tools and functions
offered by a system.
For example, those who want to modify a
spline curve by programming it would
have an easier time doing so if they have
used the system spline command already.
What does it take to program a
CAD system?
• We need to know a programming
language, geometry and graphics
concepts, and database techniques. We
may need more or less of each of these,
depending on the programming or
customization task at hand. For a
programming language, C, C++, Visual
basic or Java is good.
Visual Basic

C++
Relational and Object Databases
• CAD systems utilize relational databases
to store geometric, graphical, and other
data that defines a CAD model. A model
database is stored in the model or part file.
• A relational database is conceptually a
set or collection of tables. A table is
considered a relation. A database with
four tables is considered a database with
four relations. Each table stores data
about a given entity. The data in a table is
organized or stored as attributes in rows.
Each row in a table is known as a record.
• For example, a database of a bank may
have a table of customers (the entity).
Each customer has a row (record) in the
table. The record could hold the customer
name and account number (the data
attributes). Entities can be thought of as
objects with attributes.
Relationships between tables can
be of three types
• one-to-one, one-to-many, and many-to-one.
• In a one-to-one relationship, there is only one
record in the second table for each record in the
first table. In the one-to-many relationship,
multiple records exist in the second table for
each record in the first table. The one-to-many
relationship is sometimes known as a parent-
child relationship. The many-to-one relationship
is opposite to the one-to-many relationship; for
multiple records in the first table, only one record
exists in the second table.
• SQL (Structured Query Language) is the language
commonly used to query and update relational
databases.
• SQL can be used to retrieve, sort, and filter specific data
of a database. SQL also allows users to define data in
databases and later manipulate it. SQL was developed
by IBM.
• SQL uses set logic to query databases. It is an easy
language to learn; it has an English-like syntax.
• It requires few keywords and clauses to perform
powerful data retrieval from the database.
• Report formatting is also simple using SQL. Sample
statements include CREATE DATABASE, CREATE
TABLE, SELECT, ALTER, INSERT, DELETE, and
UPDATE.
• The query SELECT FirstName, LastName
FROM CustomerName; returns the two columns
labeled FirstName and LastName in the table
CustomerName as the query result. Note that
the SELECT statement must end with a
semicolon, as all SQL statements end with a
semicolon.
• We must create a database before we can use
it. We need a database schema that defines the
types of variables used in the database records
in each table. We use this schema to create
each table of the database. The following
screenshot (of Microsoft Access) shows the
definition of a schema for a bank database table.
CAD systems utilize various types of data structures and
databases to store CAD menu and texts and their related
data. Many systems use relational databases to store
model data. They store the boundary representation of
the model, that is, the faces, edges, and vertices of the
model.

In such as a database, we use three relational tables:


Faces, Edges, and Vertices as shown in Figure 5.1. The
Faces table links (points) to the Edges table, and the
Edges table links (points) to the Vertices table. Each of
the three tables has one record per entity (face, edge, or
vertex). Each record has the geometric definition of each
entity and other information, such as color and layer, that
we do not show in the tables.
Object Definition
• The basic unit of thinking, designing, building, and
programming code is the object. Let us consider a define
what an object is. If we think of what characterizes a car,
we come up with make, model, year, color, drive, stop,
maintain, and so forth. We can further divide these
characteristics into two groups: attributes (properties)
and behaviors. The attributes are make, model, year,
and color. An attribute is just a value that we specify; it
does not require any calculations — the year is 2005 and
the color is red. Object attributes become its variables in
programming.
The behaviors are drive, stop, and
maintain. A behavior is doing something;
it takes input and produces output. Driving
a car requires a sequence of steps. Think
of a behavior as an algorithm. Object
behaviors become its methods in OOP.
A method is a function associated with an
object.
• An object is defined as an entity (construct) that
has attributes and behaviors. The attributes are
its variables, and the behaviors are its methods.
• CAD programming utilizes OOP languages. We
use these languages to create user-defined
objects, or we use their built-in (predefined)
objects provided by CAD systems.
Understanding objects is a prerequisite to
creating one's (custom) objects or using existing
(predefined) ones.
Define objects.
Write the definition of a spur gear
Each of these models is an object that has attributes and behaviors.
Table 5.1 shows some of them. It also shows their mapping to OOP
variables and methods.
• Spur gear object = {{root diameter, base diameter, addendum
diameter, thickness, number of teeth}, {rotate, maintain}}

Figure 2.7
Parametric Design and Objects

The philosophy of parametric modeling and design goes


hand-in-hand with OOP. As shown in Figure 2.7, a
parametric solid model is defined by its parameters P1 to
P4, R, and an extrusion thickness T (not shown in the
figure). The different values of these six parameters
define a family of parts; each part in the family has
specific values for the six parameters (variables). A CAD
designer creates the solid sketch shown in Figure 2.7.
When the designer sets the values of the parameters
and regenerates the solid, a new part in the family is
created. The six parameters define the attributes
(variables) of the class. In this case, there are no
methods for the class. Each part in the family is an
instance of the class. Each part has specific values for
the six parameters.
• Each time a designer assigns specific values to the six
parameters of the Extrude class to create a part, the
CAD system creates an instance — extrude1, extrude2,
and so forth. When the designer deletes the part, the
CAD system deletes the instance. However, the Extrude
class is still there and can be instantiated to create a
new part.
• When the CAD designer clicks a parameter to assign it a
new value (dimension), the designer is using the object
concept of membership access. For example, selecting
P1 of the instance extrude1 is equivalent to calling the
extrude1. setPl { } method. Changing the current value of
P1 to, for example, 5, and hitting Enter or OK is
equivalent to calling extrude1.setPl(5).
Programming Levels
The CAD programming can be divide into two
groups.
a. The first group is programming the three-
dimensional geometric and graphics concepts.
b. The second group is programming the CAD
systems themselves.
Programming the first group is a prerequisite to
programming the second group because we
need to learn the programming concepts without
having to deal with the high level of
sophistication of the programming interfaces of
CAD systems and the complex data structure
and large size.
We can use one of two approaches to program
the first group: programming languages or
symbolic equation solvers. The approach of
programming languages requires a
programming language such as C, C++, or Java,
and a rendering system to display the modeling
and graphics results in 3D space. Two major
rendering systems exist: OpenGL and Java 3D.
The OpenGL-based system is used with C or
C++. We write a C or C++ program to code the
geometric and graphics concepts, and we call
functions from the OpenGL library to render the
results.
• The approach of symbolic equation solvers requires
software packages that allow us to manipulate and solve
the equations of geometric modeling and graphics
concepts. These symbolic equation solvers include
MATLAB, Maple, Mathcad, and Mathematica. The use of
symbolic equation solvers is quite common in
engineering practice. These solvers are used for
teaching and research in various disciplines in academia
and industry.
• A symbolic equation solver solves differential and
integral equations and performs linear algebra, including
vector manipulations. It also solves matrix (system of)
equations and performs matrix computations such as
adding, multiplying, and inverting matrices.
• The second set of CAD programming
tasks involves programming CAD systems
themselves. This group builds on what we
learned about programming the geometry
and graphics.
Macros
• A macro is a simple string of commands that records a
sequence of actions that a designer performs from the
time the designer turns on the macro command on a
CAD system until it is turned off. The macro is saved in a
file that can be played back to execute the sequence of
commands. Macros are good to use to automate
repetitive design tasks. Some macros can be edited to
change parameters of a feature, thus enabling the
designer to create a family of parts.

• Different CAD systems use different languages to create


macro files. For example, SolidWorks uses VB (Visual
Basic) to record macros. We usually perform three
activities: create, run, and edit. We first create a macro,
and then run it. If necessary, we edit the macro file to
make changes, save the file and run it again. Think of a
macro as a simple computer program. Moreover, CAD
systems allow their users to create a button to access
the macro.
Example: Create, run, and edit macros.
Show an example of creating, running, and editing a
macro.

• Follow these steps:


1. Create macro. Turn on the macro command to begin
recording. For example, click this sequence, or its
equivalent: Tools (menu on menu bar) => Macro =>
Record.
2. Perform activities. Begin geometric modeling as if macro
were not on. For example, select sketch planes, draw a
sketch, and extrude it to create a feature.
Start and create macros

Begin geometric modeling


3. Stop macro and save it. Stop the macro
recording by turning the macro off. Click
this sequence, or its equivalent: Tools
(menu on menu bar) => Macro => Stop. A
window opens up asking for a macro File-
name and a directory to save the macro
in.
4. Exit the part without saving. This way
we can run the macro to test it.
Macros and its program
5. Run the macro. Open a new part file.
Then run the macro. The macro should
execute its sequence of commands and
create the extruded feature in the open
part file. For example, click this sequence,
or its equivalent: Tools (menu on menu
bar) => Macro => Run. A dialog box
opens to allow the selection of the macro,
in case more than one exists.
Run macros
6. Assign a toolbar button to a macro. If
needed, follow the system directions to
add a macro button to the CAD system
toolbar. For example, click this sequence,
or its equivalent: Tools (menu on menu
bar) => Customize => Macros (tab).
Select the desired macro from the popup
window shown in the screen-shot at right.
After the procedure is complete, the macro
is added to the toolbar.
7. Edit macro. If necessary, a macro can be edited if we
know the language it is written in. We open its file, edit it,
save it, and run it again. For example, click this
sequence, or its equivalent: Tools (menu on menu bar) =
> Macro => Edit. After you select the macro to edit, its
file opens up in an editor for editing.
For example, SolidWorks uses Microsoft Visual Basic
editor. Recognize the VB statements and the SolidWorks
API calls. Other systems use system editors. For
example, Pro/E uses the vi editor on Unix systems.
Users who know the APIs well can produce the macro
files by writing the code manually.

*application programming interface (API)


This example shows how to deal with macros in a system
Independent fashion. As the example shows, dealing with a
macro on a CAD system is an easy task to accomplish.
Example. Hands-on exercise:
Apply the preceding seven macro steps to
your CAD system to record the creation of
an extruded feature using a rectangular
cross section. Change the macro to
accept input from the user for the
rectangle's width and height, as well as
the extrusion depth. For example,
CATIA lets users use VB forms to receive
input from them.
Thank you

You might also like