| 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. How To Support This SiteTutorials 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:
IntroductionThe 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:
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 UnitsAlibre 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. ' Internal Units & other variables Dim dimension As Double = 2.54D ' 2.54 cm = 1 in. Dim filePath As String = "c:/" Dim partName As String = "MyCube" // Internal Units & other variables double dimension = 2.54D; // 2.54 cm = 1 in. string filePath = "c:/"; string partName = "MyCube"; Connecting To Alibre DesignA 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. ' Connect to Alibre Design
Dim hook As AutomationHook _
= Marshal.GetActiveObject("AlibreX.AutomationHook")
Dim root As IADRoot = hook.Root
// Connect to Alibre Design
AutomationHook hook = (AutomationHook)Marshal
.GetActiveObject("AlibreX.AutomationHook");
IADRoot root = (IADRoot)hook.Root;
Creating a New PartNow 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. ' Create a New Part Dim part As IADPartSession _ = root.CreateEmptyPart(partName, False) part.Color = Color.FromArgb(255, 0, 0).ToArgb() part.Reflectivity = 50 // Create a New Part IADPartSession part = root.CreateEmptyPart(partName, false); part.Color = Color.FromArgb(255, 0, 0).ToArgb(); part.Reflectivity = 50; Creating A SketchFirst 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. ' Get the first design plane Dim design As IADDesignSession = part Dim plane As IADDesignPlane = = design.DesignPlanes.Item(0) ' first plane // Get the first design plane IADDesignSession design = (IADDesignSession)part; IADDesignPlane plane = design.DesignPlanes.Item(0); // first plane Now we'll add a sketch based on the plane, give it an appropriate name. ' Add a sketch Dim sketch As IADSketch _ = part.Sketches.AddSketch(Nothing, plane, "Square") // Add a sketch IADSketch sketch = part.Sketches.AddSketch(null, plane, "Square"); 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. ' Draw the square ' allow us to center about the origin Dim offset As Double = dimension / 2D Call sketch.BeginChange() sketch.Figures.AddRectangle(-offset, _ -offset, offset, offset) Call sketch.EndChange() // Draw the square // allow us to center about the origin double offset = dimension / 2D; sketch.BeginChange(); sketch.Figures.AddRectangle(-offset, -offset, offset, offset); sketch.EndChange(); Creating an Extrusion FeatureNow 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. ' Create an Extrusion Feature Dim session As IADSession = part Dim extrusion As IADExtrusionFeature _ = part.Features.AddExtrudedBoss( sketch, dimension, _ ADPartFeatureEndCondition.AD_MID_PLANE, _ Nothing, Nothing, 0D, ADDirectionType.AD_ALONG_NORMAL, _ Nothing, Nothing, False, 0, False, "MyCube", _ "The Depth Parameter", Nothing) Call session.Parameters.OpenParameterTransaction() extrusion.DepthParameter.comment _ = "This is an important feature." Call session.Parameters.CloseParameterTransaction() // Create an Extrusion Feature IADSession session = (IADSession)part; IADExtrusionFeature extrusion = part.Features.AddExtrudedBoss( sketch, dimension, ADPartFeatureEndCondition.AD_MID_PLANE, null, null, 0.0D, ADDirectionType.AD_ALONG_NORMAL, null, null, false, 0, false, "MyCube", "The Depth Parameter", null); session.Parameters.OpenParameterTransaction(); extrusion.DepthParameter.comment = "This is an important feature."; session.Parameters.CloseParameterTransaction(); Saving and Closing the PartWe 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. ' Save and Close the Part Dim temp As Object = filePath Call session.SaveAs(temp, partName) Call session.Close(False) // Save and Close the Part object temp = (object)filePath; session.SaveAs(ref temp, partName); session.Close(false); Cleaning UpWe 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. ' Clean Up Call Marshal.ReleaseComObject(hook) Call GC.GetTotalMemory(True) // Clean Up Marshal.ReleaseComObject(hook); GC.GetTotalMemory(true);
|
|
| Last Updated ( Monday, 28 July 2008 00:36 ) |
