Help me go to college!
Discussion Forums
Join the discussion forums today and help the community grow by contributing
your questions, comments, ideas, and expertise!
Join now!
Google Search
|
Alibre API Programming Introduction
Getting started with the Alibre Design API can be tricky for a beginner.
Here's some sample code in both VB and C# to get you started.
All images, text and code is ©1995-2006 by Alex Franke.
All rights reserved.
Published: July 23, 2006
Updated: July 23, 2006
In this article:
How To Support This Site
Tutorials like these take a good deal of time and energy, and as you can probably
imagine, the audience is quite small. There are a couple of ways you can support these
efforts, but the easiest way to do it is to help this site grow by browsing the
articles and applications, and encouraging your friends and associates to do the same.
You can also click on "Donate Now" in the yellow "Help Me Go To College" area to make
a monetary contribution. (These donations are safe and secure and go into my childrens' college funds.)
I have a lot of applications, articles, and links online, so if you see something you
like, please contribute and spread the word! Thank you!
If you like this article, you or your friends might also like:
Introduction
The goal of this tutorial is to use the Alibre Design API to
connect to a running instance of Alibre Design and create a new part
file containing a shiney, blue, one-inch cube cenetered
about the origin. This is targeted to a beginner audience and will
touch on the following topics:
- Internal units
- Connecting to Alibre Design
- Creating a New Part
- Creating a Sketch
- IADDesignSession
- IADDesignPlane
- IADSketch
- [Begin/End]Change()
- Creating a Feature
- IADSession
- IADExtrusionFeature
- [Open/Close]ParameterTransaction()
- Saving and Closing
- Cleaning Up
The line numbers are for reference only -- you should not type them into the program.
To follow along with this tutorial you will need to have Alibre Design
(or Alibre Design Xpress) installed and running on your machine. You will
also need to have a Microsoft Visual Studio product such as Visual Basic Express,
which is available from Microsoft free of charge, at least for a limited time.
This tutorial will also give you an idea of how similar VB.Net and C#.Net are. Using Alibre Design
does not require VB specifically. Any programming language that can integrate COM objects will do the trick.
Internal Units
Alibre Design works in centimeters internally, so we much convert
our cube dimension accordingly. It doesn't matter what units the
user prefers because Alibre Design will convert to those units
from centimeters automatically. We just need to get the measurements
into Alibre Design in centimeters. We'll also set up some other data
here.
VB Code
1: ' Internal Units & other variables
2: Dim dimension As Double = 2.54D ' 2.54 centimeters is one inch.
3: Dim filePath As String = "c:/"
4: Dim partName As String = "MyCube"
5:
|
C# Code
1: // Internal Units & other variables
2: double dimension = 2.54D; // 2.54 centimeters is one inch.
3: string filePath = "c:/";
4: string partName = "MyCube";
5:
|
Connecting To Alibre Design
A hook essentially enables communication between different
applications. Alibre Design provides this by way of the
AlibreX.AutomationHook COM object, which is installed and registered
on your system when you install Alibre Design. We want to connect to a
running instance of Alibre Deisgn, so we'll access the Running Objects
Table (ROT) via the GetActiveObject() method, and cast the result to
the Alibre AutomationHook. (You can also start up Alibre Design if it's
not already running, but that's out of scope for this example.) Then
we'll grab a reference to the hook's IADRoot, which is basically like
the "Home" window of Alibre Design, enabling us to create parts,
assemblies, etc.
VB Code
6: ' Connect to Alibre Design
7: Dim hook As AutomationHook = Marshal.GetActiveObject("AlibreX.AutomationHook")
8: Dim root As IADRoot = hook.Root
9:
|
C# Code
6: // Connect to Alibre Design
7: AutomationHook hook = (AutomationHook)Marshal.GetActiveObject("AlibreX.AutomationHook");
8: IADRoot root = (IADRoot)hook.Root;
9:
|
Creating a New Part
Now we'll create the part and assign a couple of its properties.
Note that we're converting from an RGB value of (255,0,0) to a
BGR value as an integer. We do this because Alibre Design likes to think of
colors in terms of blue-green-red sets of values, and the normal order you
see working in Windows is red-green-blue.
VB Code
10: ' Create a New Part
11: Dim part As IADPartSession = root.CreateEmptyPart(partName, False)
12: part.Color = Color.FromArgb(255, 0, 0).ToArgb()
13: part.Reflectivity = 50
14:
|
C# Code
10: // Create a New Part
11: IADPartSession part = root.CreateEmptyPart(partName, false);
12: part.Color = Color.FromArgb(255, 0, 0).ToArgb();
13: part.Reflectivity = 50;
14:
|
Creating A Sketch
First let's get a design plane to work with. Because we're making a cube that's
centered on the origin, we can just use the first plane (probabaly XY-Plane).
There are tips for tracking down the localized names of planes in the API
documentation, but for now we'll just stick with this. We'll start by getting
IADDesignSession interface from the part.
VB Code
15: ' Get the first design plane
16: Dim design As IADDesignSession = part
17: Dim plane As IADDesignPlane = design.DesignPlanes.Item(0) ' first plane
18:
|
C# Code
15: // Get the first design plane
16: IADDesignSession design = (IADDesignSession)part;
17: IADDesignPlane plane = design.DesignPlanes.Item(0); // first plane
18:
|
Now we'll add a sketch based on the plane, give it an appropriate name.
VB Code
19: ' Add a sketch
20: Dim sketch As IADSketch = part.Sketches.AddSketch(Nothing, plane, "Square")
21:
|
C# Code
19: // Add a sketch
20: IADSketch sketch = part.Sketches.AddSketch(null, plane, "Square");
21:
|
Now draw the square. Note that we must call BeginChange() and EndChange().
These methods in effect "activate" the sketch for 2D drawing. You can
ALT-TAB over to the Alibre Design part during all of this and actually see
it working on these commands.
VB Code
22: ' Draw the square
23: Dim offset As Double = dimension / 2D ' allows us to center about the origin
24: Call sketch.BeginChange()
25: sketch.Figures.AddRectangle(-offset, -offset, offset, offset)
26: Call sketch.EndChange()
27:
|
C# Code
22: // Draw the square
23: double offset = dimension / 2D; // allows us to center about the origin
24: sketch.BeginChange();
25: sketch.Figures.AddRectangle(-offset, -offset, offset, offset);
26: sketch.EndChange();
27:
|
Creating an Extrusion Feature
Now we'll create a simple extrusion feature and add a comment to its
parameter while we're at it. Note that parameters must only be changed
from between OpenParameterTransaction() and CloseParameterTransaction()
calls, which are part of the Parameters collection of the part's
IADSession interface.
VB Code
28: ' Create an Extrusion Feature
29: Dim session As IADSession = part
30: Dim extrusion As IADExtrusionFeature = part.Features.AddExtrudedBoss( _
31: sketch, dimension, ADPartFeatureEndCondition.AD_MID_PLANE, _
32: Nothing, Nothing, 0D, ADDirectionType.AD_ALONG_NORMAL, _
33: Nothing, Nothing, False, 0, False, "MyCube", _
34: "The Depth Parameter", Nothing)
35: Call session.Parameters.OpenParameterTransaction()
36: extrusion.DepthParameter.comment = "This is an important feature."
37: Call session.Parameters.CloseParameterTransaction()
38:
|
C# Code
28: // Create an Extrusion Feature
29: IADSession session = (IADSession)part;
30: IADExtrusionFeature extrusion = part.Features.AddExtrudedBoss(
31: sketch, dimension, ADPartFeatureEndCondition.AD_MID_PLANE,
32: null, null, 0.0D, ADDirectionType.AD_ALONG_NORMAL,
33: null, null, false, 0, false, "MyCube", "The Depth Parameter", null);
34: session.Parameters.OpenParameterTransaction();
35: extrusion.DepthParameter.comment = "This is an important feature.";
36: session.Parameters.CloseParameterTransaction();
37:
|
Saving and Closing the Part
We also use the session interface to save and close the part.
Note the explicit "boxing" of the string into an object to ensure
compatability with the SaveAs() method.
VB Code
39: ' Save and Close the Part
40: Dim temp As Object = filePath
41: Call session.SaveAs(temp, partName)
42: Call session.Close(False)
43:
|
C# Code
38: // Save and Close the Part
39: object temp = (object)filePath;
40: session.SaveAs(ref temp, partName);
41: session.Close(false);
42:
|
Cleaning Up
We late-bound a COM object, so we really should clean up a bit when
we're finished with it. The call to GC will allow the garbage collector
to do a little housekeeping.
VB Code
44: ' Clean Up
45: Call Marshal.ReleaseComObject(hook)
46: Call GC.GetTotalMemory(True)
|
C# Code
43: // Clean Up
44: Marshal.ReleaseComObject(hook);
45: GC.GetTotalMemory(true);
|
|