Infrastructure Getting Started with Automation
This article will show you how to use a scripting language to access CAA V5 automation objects to capture your own know-how and to increase your productivity. You can customize V5 applications to automate repetitive tasks, and to make it fit your own process.
The products that make up the CATIA and DELMIA applications share the same object model which can be accessed, as well as their own objects, by scripts written in Visual Basic with Windows, and scripts written in Basic Script for UNIX.
You can write your scripts from scratch, but you can also use the journalling facility from the
Macros ... command in the Tools menu that records end-user scenarios in scripts you can then use as is or modify.
Recording macros is not available in all workbenches.
We put here a simple script example, to show what is scripting, which are the different things to do to script, present briefly the scripting environment and dialog window, and what is journaling. This example is divided into the following parts:
•
1-Recording the scenario, where we'll record the creation of a cylindric pad.•
2-Modifying the generated macro, where we'll modify the generated macro to create five similar pads•
3-Replaying the modified macroYou will then find information about the scripting languages and environments, and some keys for you if you are not familiar with writing macros in Invoking CATIA from a Scripting Language.
1-Recording the Scenario
This scenario creates a circle in a sketch, and uses this sketch to create a cylindric pad. The recorded macro is stored in a file, and not in the document.
1.
Select the Tools menu, point to Macro and click Start Recording ... to display the Record Macro dialog box.2.
In the Macro dialog box, click in the Macro in area to External File to store the macro in a file. Give a name to the macro. To do this, click Select. The Select External File dialog box appears. Select or create the appropriate file and click Open.3.
Click Start in the Record Macro dialog box to start recording the macro. The Stop Recording dialog box appears. Leave it as is until the last step is reached.4.
In the File menu, click on New, or click on the icon, and double-click Part to create a new part. A new part is created and a window for this part is opened.5.
Select the xy plane in the specification tree, and select the sketcher icon to create a sketch.6.
In the sketcher toolbar, select the circle icon and click twice to successively indicate the center of the circle and a current point on the circle.7.
Click on the sketcher exit icon8.
Select the pad icon to create a pad on the sketch.9. In the Pad Definition dialog box, choose a length of 20 mm and click OK. The pad is created.
11.
Click Stop Recording in the Stop Recording dialog box, or , in the Tools menu, point to Macro and click Stop Recording . Your macro is now stored in the file you have selected.Have a look at the generated macro: Language="VBSCRIPT"
Sub CATMain()
Dim documents1 As Documents Set documents1 = CATIA.Documents Dim partDocument1 As Document
Set partDocument1 = documents1.Add("Part") Dim part1 As Part
Set part1 = partDocument1.Part Dim bodies1 As Bodies
Set bodies1 = part1.Bodies Dim body1 As Body
Set body1 = bodies1.Item("MechanicalTool.1") Dim sketches1 As Sketches
Set sketches1 = body1.Sketches Dim originElements1 As OriginElements Set originElements1 = part1.OriginElements Dim reference1 As AnyObject
Set reference1 = originElements1.PlaneXY Dim sketch1 As Sketch
Set sketch1 = sketches1.Add(reference1) Dim arrayOfVariantOfDouble1(8) arrayOfVariantOfDouble1(0) = 0.000000 arrayOfVariantOfDouble1(1) = 0.000000 arrayOfVariantOfDouble1(2) = 0.000000 arrayOfVariantOfDouble1(3) = 1.000000 arrayOfVariantOfDouble1(4) = 0.000000 arrayOfVariantOfDouble1(5) = 0.000000 arrayOfVariantOfDouble1(6) = 0.000000 arrayOfVariantOfDouble1(7) = 1.000000 arrayOfVariantOfDouble1(8) = 0.000000 sketch1.SetAbsoluteAxisData arrayOfVariantOfDouble1 Dim factory2D1 As Factory2D
Set factory2D1 = sketch1.OpenEdition()
Dim geometricElements1 As GeometricElements Set geometricElements1 = sketch1.GeometricElements Dim axis2D1 As GeometricElement
Set axis2D1 = geometricElements1.Item("AbsoluteAxis") Dim line2D1 As AnyObject
Set line2D1 = axis2D1.GetItem("HDirection") line2D1.ReportName = 1
Dim line2D2 As AnyObject
Set line2D2 = axis2D1.GetItem("VDirection") line2D2.ReportName = 2
Dim circle2D1 As Circle2D
Set circle2D1 = factory2D1.CreateClosedCircle(0.000000, 0.000000, 10.000000) Dim point2D1 As AnyObject
Set point2D1 = axis2D1.GetItem("Origin") circle2D1.CenterPoint = point2D1
circle2D1.ReportName = 3 sketch1.CloseEdition part1.Update
Dim shapeFactory1 As Factory
Set shapeFactory1 = part1.ShapeFactory Dim pad1 As Pad
Set pad1 = shapeFactory1.AddNewPad(sketch1, 20.000000) part1.Update
End Sub
We'll detail below, line by line, what has been recorded, following the interactive steps:
1.
Startintg to record a macro creates the macro file and generates the first instruction stating the scripting language used and the macro entry point, the CATMain sub:Language="VBSCRIPT" Sub CATMain()
2.
Click on the New item of the File menu, or click on the icon , and double-click Part to create a new part generates the following instructions:3. Dim documents1 As Documents 4. Set documents1 = CATIA.Documents 5.
6. Dim partDocument1 As Document
7. Set partDocument1 = documents1.Add("Part")
A new document with the Part type is created. To do this, such a document is added to the Documents collection of the CATIA application.
9. Dim part1 As Part
10. Set part1 = partDocument1.Part 11.
12. Dim bodies1 As Bodies 13. Set bodies1 = part1.Bodies 14.
15. Dim body1 As Body
Set body1 = bodies1.Item("MechanicalTool.1") Dim sketches1 As Sketches
Set sketches1 = body1.Sketches Dim originElements1 As OriginElements Set originElements1 = part1.OriginElements Dim reference1 As AnyObject
Set reference1 = originElements1.PlaneXY Dim sketch1 As Sketch
Set sketch1 = sketches1.Add(reference1) Dim arrayOfVariantOfDouble1(8) arrayOfVariantOfDouble1(0) = 0.000000 arrayOfVariantOfDouble1(1) = 0.000000 arrayOfVariantOfDouble1(2) = 0.000000 arrayOfVariantOfDouble1(3) = 1.000000 arrayOfVariantOfDouble1(4) = 0.000000 arrayOfVariantOfDouble1(5) = 0.000000 arrayOfVariantOfDouble1(6) = 0.000000 arrayOfVariantOfDouble1(7) = 1.000000 arrayOfVariantOfDouble1(8) = 0.000000 sketch1.SetAbsoluteAxisData arrayOfVariantOfDouble1 Dim factory2D1 As Factory2D
Set factory2D1 = sketch1.OpenEdition()
A Sketch object named Sketch1 is added to the Sketches collection using the reference1 Reference corresponding to the XY plane as a support. Using a reference allows to create a sketch either on an element, as here the XY plane, or on a solid planar face that is not directly accessible as a VB object.
The SetAbsoluteAxisData method is used to define the orientation of the sketch axis, that can be on either side and can rotate inside of the support plane. A Factory2D object is created by opening the sketch editor against the created sketch. This Factory2D object features methods to create 2D objects.
Dim geometricElements1 As GeometricElements Set geometricElements1 = sketch1.GeometricElements Dim axis2D1 As GeometricElement
Set axis2D1 = geometricElements1.Item("AbsoluteAxis") Dim line2D1 As AnyObject
Set line2D1 = axis2D1.GetItem("HDirection") line2D1.ReportName = 1
Dim line2D2 As AnyObject
Set line2D2 = axis2D1.GetItem("VDirection") line2D2.ReportName = 2
When the sketch is created, an axis, that is the aggregation of a center point, and horizontal line and vertical line (directions), is created.
The axis is retrieved in the GeometricElements collection of the Sketch object, the directions are retrieved as objects aggregated by the axis. The two lines are here assigned an identifier using their ReportName property that will be used by the 3D modeling services to retrieve those elements inside of the sketch. They have no end-user meaning.
16.
In the sketcher toolbar, select the circle icon and click twice to indicate successively the center of the circle and a current point on the circle17. Dim circle2D1 As Circle2D
18. Set circle2D1 = factory2D1.CreateClosedCircle(0.000000, 0.000000, 10.000000) 19.
20. Dim point2D1 As AnyObject
21. Set point2D1 = axis2D1.GetItem("Origin") 22.
23. circle2D1.CenterPoint = point2D1 24.
25. circle2D1.ReportName = 3
The CreateCloseCircle method of the Factory2D object is used to create the circle. It is first created as centered at the point (0,0) with a radius of 10 mm. It is then constraint on the axis center point using the CenterPoint property.
26.
Click on the sketcher exit icon 27. CATIASketch3.CloseEdition 28. part1.UpdateThe sketch editor is closed and the part udapted.
29.
Select the pad icon to create a pad, and in the Pad Definition dialog box, choose a length of 10 mm and click OK. The pad is created.30. Dim shapeFactory1 As Factory
31. Set shapeFactory1 = part1.ShapeFactory 32.
33. Dim pad1 As Pad
34. Set pad1 = shapeFactory1.AddNewPad(sketch1, 20.000000) 35.
36. part1.Update 37.
The AddNewPad method of the ShapeFactory object is used to create the pad. It is created using the sketch and the length of 20mm. The part is updated.
38.
Click Stop Recording in the Stop Recording dialog box, or point to Stop Recording in the Macro item of the Tools menu.This closes the macro recording sequence and saves the macro in the selected file. This is all what you performed interactively.
This task explains how to modify the generated macro to make it loop on the creation of five identical cylindric pads.
1.
In the Tools menu, point to Macro and click Macros... to display the Macro dialog box.2.
In the Macro dialog box, click in the "Macro in" area and select "External File".3.
Select the name of the macro to edit. If the recorded macro is not already the current one, click Select and the Select External File dialog box appears. Click the appropriate file name and click Open.4.
Click Edit. Your favorite editor opens on the selected macro. The instructions written using the bold typeface are those you need to add or modify while the others already exist in the macro:5. Language="VBSCRIPT"
'My macro creates five cylinders Sub CATMain()
Dim documents1 As Documents Set documents1 = CATIA.Documents ...
Dim refer1 As AnyObject
Set refer1 = originElements1.PlaneXY x = 0 Dim arrayOfVariantOfDouble1(8) arrayOfVariantOfDouble1(0) = 0.000000 arrayOfVariantOfDouble1(1) = 0.000000 arrayOfVariantOfDouble1(2) = 0.000000 arrayOfVariantOfDouble1(3) = 1.000000 arrayOfVariantOfDouble1(4) = 0.000000 arrayOfVariantOfDouble1(5) = 0.000000 arrayOfVariantOfDouble1(6) = 0.000000 arrayOfVariantOfDouble1(7) = 1.000000 arrayOfVariantOfDouble1(8) = 0.000000 For I = 1 To 5
You can choose your own text editor to edit the macro by setting the
CATMacroEditor environment variable prior to launching CATIA with the name of the editor program:
set CATMacroEditor=NOTEPAD or using Control
Panel/System/Environment on Windows, or:
export CATMacroEditor=vi
On Unix. This editor must be accessible through the PATH environment variable. Consult your administrator for more information on how to proceed.
Dim sketch1 As Sketch
Set sketch1 = sketches1.Add(refer1) ...
Dim circle2D1 As Circle2D Set circle2D1 = _ factory2D1.CreateClosedCircle( _ x, _ 0.000000, _ 10.000000) circle2D1.ReportName = 3 sketch1.CloseEdition ... part1.Update x = x + 25 Next End Sub
You simply need to initialize a variable, here x, to allow for the sketch position in the plane to vary, and create a loop beginning with the For keyword and ending with the Next keyword. The For keyword specifies the counter variable I which will take all values between 1 and 5 inclusively. Move the array declaration and valuation outside of the loop: those values do not change. Change the first parameter of the CreateCloseCircle method to x. Increment the value of the x variable to move the next center of 25mm from the previous one.
5. Save the macro as MyMacro.
The source of the modified macro, CAAInfGettingStarted.CATScript, is available in the CAAScdInfUseCases module. Execute macro (windows only).
3-Running the Macro
This task explains how to run the modified macro.
1.
In the Tools menu, point to Macro and click Macros... to display the Macro dialog box.2.
Your macro should be the current one. You just have to click Run to run this macro. Here is the result.In Short
This use case has shown how to record a macro, modify it and then launch its execution.
[Top]
References [1
] Adding a macro in a toolbar
[Top]
Infrastructure
Invoking CATIA from a Scripting Language
Access to the CATIA object model is provided using scripts in different ways depending on
the operating system and on the applications that can share their own objects with CATIA.
CATIA is an OLE Automation server for Windows NT and allows macro record and replay for
both Windows NT and UNIX. The following summarizes CATIA scripting capabilities.
With
Windows
:
•
In-process access using Visual Basic Scripting Edition since
CATIA hosts the Visual Basic scripting engine
•
Out-process access from Visual Basic for Applications via
applications like those of Office
•
Out-process access from Visual Basic 5 Development Studio
•
Out-process access using the Windows Scripting Host and
scripting languages such as VB Script or JScript
•
Out-process access from a html page
interfaces through the Invoke method.
With
UNIX:
•
in-process access using Summit's Basic Script
The macros recorded from the Tools menu and the Record Macro dialog box use the Visual
Basic language, and not VBScript, to be compatible with Basic Script, allowing macro
portability between UNIX and Windows NT, that is a macro recorded on Windows NT can be
replayed on UNIX or the reverse. This means for example that Dim statements are recorded
to declare objects as returned values of properties or methods.
In-process access means that the script interpretation is performed in the same process as
CATIA. You usually run the macros from the Macros window triggered from the interactive
Tools->Macros command. In this case, the macro is processed by CATIA just like any other
command.
Out-process access means that you run the macro from another application running in
another process. In this case, the macro should first connect to CATIA to then access its
data. This connection starts CATIA if no CATIA process is being running.
You can find information about in-process and out-process access in:
•
Running In-process Macros
•
Running Out-process Macros
Running In-process Macros
In-process access means that the script interpretation is performed in the same process as
CATIA using the scripting engine(s) hosted by CATIA. You can run in-process macros with
UNIX and Windows. You have three means to run in-process macros:
1.
You usually run the macros from the Macros window triggered from the interactive
Tools->Macros command. In this case, the macro is processed by CATIA just like
any other command.
2. You can start CATIA and request that a macro being executed as soon as CATIA is
started using the -macro option followed by the full path of the macro you want to
run:
CNEXT -macro e:\Users\Macros\MacroToRun.CATScript
3. You can start CATIA in batch to execute a macro using the -batch option followed by
the full path of the macro you want to run:
CNEXT -batch e:\Users\Macros\BatchMacro.CATScript
Running Out-process Macros
Out-process access means that you run the macro from another application running in
another process, such as from Visual Basic for Applications associated with products such as
Excel or Word, or from Microsoft Visual Basic 5 Development Studio. You can also use the
Windows Scripting Host to run VBScript or JScript macros by simply double clicking the
macro name from the Windows desktop or Explorer, or from the command console. You can
finally use VBScript or JScript macros embedded in html pages.
The macro should first connect to CATIA to then access its data. This connection starts CATIA
if no CATIA process is being running. The script is interpreted by the scripting engine hosted
by the application from which you start the macro.
You can run out-process macros with Windows NT only.
Running Out-process Macros from VBA or VB5
This applies for scripts written in Visual Basic only
•
If CATIA is already running, the macro should simply connect to CATIA using the
GetObject method
•
Dim CATIA As ObjectSet CATIA = GetObject(, "CATIA.Application")
The first argument is left blank.
•
If CATIA is not already running, the macro should start CATIA using the
CreateObject method
•
Dim CATIA As ObjectSet CATIA = CreateObject("CATIA.Application")
Running Out-process Macros Using the Windows Scripting Host
Another way is to use the Windows Scripting Host. This is a language-independent scripting
host which enables scripts written in different languages such as Visual Basic, JScript, and
Perl, to be run from the Windows desktop, the Windows Explorer, or the command console.
•
With Visual Basic, your script should begin by the connection to CATIA, using either
CreateObject or GetObject, as follows:
•
Dim CATIASet CATIA = WScript.CreateObject("CATIA.Application")
or
Dim CATIA
Set CATIA = WScript.GetObject("", "CATIA.Application")
•
With JScript, your script should also begin by the connection to CATIA, using either
CreateObject or GetObject, as follows:
•
var CATIACATIA = WScript.CreateObject("CATIA.Application")
or
var CATIA
Note that the GetObject method requires that its first argument be blank for both Basic
Script and JScript.
To run the macros from the Windows desktop, simply double click on the macro name.
These names are suffixed using vbs for Visual Basic, and with js for JScript.
To run the macros from the command console, use the cscript command as follows:
cscript e:\users\psr\Scripting\Sample\CATIA.jsRunning Out-process Macros from a Dynamic HTML Page
You can also run macros embedded in a html page. These macros can be written in Visual
Basic and JScript. There are several ways of embedding a macro in a html page:
•
The macro is written using the script tag and run when the page is loaded
•
The macro is written using the script tag and is included or referenced by a form,
input, body, or a (anchor) tag.
•
The macro is written using the a tag (anchor) and run as an hyperlink. This is possible
with JScript only.
Generate and helicoidal stair
Option Explicit'---' COPYRIGTH DASSAULT SYSTEMES 2000
'---Dim Language as String
Language="VBScript"
' *********************************************************************** ' Purpose: Create an helicoidal Stair
' Author: ' Languages: VBScript ' Locales: English ' CATIA Level: V5R6 ' *********************************************************************** ' ---' VB Macro ' ' Object:
' Generate and helicoidal stair ' Input data
' Origin point and helix direction
' Starting point of helix and its pitch / height definition ' Height of each Step
'
' VB macro
' Initialize input data
' Compute number of required step for the helix height
' Loop on number of step and generate the geometry for each step in a HybridBody '
' CATIA VB tools related to this sample ' Open a CATIA document and a CATPart ' Show/Noshow of wireframe and surfacic object ' Adding new Open-Body
' Create parameters and formula / Use it in geomety definition
' Catia methods required CATIAReference as input object / Generate reference for each input geometry
' Create Generative Shape Design feature
' Update geometry / Note: done at the end in order to enhance creation performances ' '---' Global Variables ' ---' Origin Points Dim X0 As Double Dim Y0 As Double Dim Z0 As Double
' Starting Point of the stair helix Dim X1 As Double
Dim Y1 As Double
Dim Z1 As Double ' Direction of the helix Dim A1 As Double
Dim B1 As Double
Dim C1 As Double
' Pitch and height of the helix Dim Pitch As Double
Dim Height As Double ' Step height value Dim StepValue As Double
'---' Main program
'---' Open a New Part
' Init global variable ' Generate geometry ' ---' Sub CATMain() ' ' Create a CATIA Part Docuument
' --- ' Creating a Part Document
Dim PartDoc As Document
Set PartDoc = CATIA.Documents.Add ( "Part" ) ' Retrieving HybridBodies collection in Part Document Dim hybridBodies1 As HybridBodies
Set hybridBodies1 = PartDoc.Part.HybridBodies ' Adding an OpenBody
Dim myHBody As HybridBody
Set myHBody = hybridBodies1.Add()
' ' Init global Values
' X0 = 0 Y0 = 0 Z0 = 0 X1 = 1000 Y1 = 0 Z1 = 0 A1 = 0 B1 = 0 C1 = 1 Pitch = 3600 Height = 3000 StepValue = 100 ' ' Declaring and setting working variables ' Dim iValide As Integer
Dim iLigne As Integer
Dim Point1 As Object
Dim Point2 As Object
Dim HelixPitch As Integer
Dim HelixHeight As Integer
Dim HauteurMarche As Integer HelixPitch = Pitch HelixHeight = Height HauteurMarche = StepValue ' ' Setting knowledge objects and variables '
' Init working knowledge parameters Dim parameters As Object
Set parameters = PartDoc.Part.parameters ' Working Parm object
Dim Parm As Object
' Init working knowledge relations Dim relations As Object
Set relations = PartDoc.Part.relations ' Working Formula object
Dim Formula As Object
' Set Parameters for stair generation
Set Parm = parameters.CreateDimension("HelixPitch", "LENGTH", Pitch)
Set Parm = parameters.CreateDimension("HelixHeight", "LENGTH", Height)
Set Parm = parameters.CreateDimension("StepHeight", "LENGTH", StepValue)
'
' Generating starting geometry (Reference points /Direction of helix /Helix) '
'
' Origin and Starting Point
Set Point1 = PartDoc.Part.HybridShapeFactory.AddNewPointCoord(X0, Y0, Z0) myHBody.AppendHybridShape Point1
Set Point2 = PartDoc.Part.HybridShapeFactory.AddNewPointCoord(X1, Y1, Z1) myHBody.AppendHybridShape Point2
' Plan horizontal XY Dim Origin As Object
Set Origin = PartDoc.Part.OriginElements
Dim Plane As Object
Set Plane = Origin.PlaneXY
Dim Ref As Object
Set Ref = PartDoc.Part.CreateReferenceFromObject(Plane)
' Direction of helix Dim Dir As Object
Set Dir = PartDoc.Part.HybridShapeFactory.AddNewDirectionByCoord(A1, B1, C1) ' Note: Another way to create direction using horizontal plane
' Set Dir = PartDoc.Part.HybridShapeFactory.AddNewDirection(Ref)
' Line for helix definition Dim Line As Object
Set Line = PartDoc.Part.HybridShapeFactory.AddNewLinePtDir(Point1, Dir, 0, HelixHeight, False) myHBody.AppendHybridShape Line
' Create formula defining Line offset value equal to helix height parameter
Set Formula = relations.CreateFormula("Formula.0", "", Line.EndOffset, "HelixHeight")
' Helix
Dim RefH1 As Object
Set RefH1 = PartDoc.Part.CreateReferenceFromObject(Line)
Dim RefH2 As Object
Set RefH2 = PartDoc.Part.CreateReferenceFromObject(Point2)
Set Helix = PartDoc.Part.HybridShapeFactory.AddNewHelix(RefH1, False, RefH2, HelixPitch, HelixHeight, False, 0, 0, False)
myHBody.AppendHybridShape Helix
Set Formula = relations.CreateFormula("Formula.1", "", Helix.Pitch, "HelixPitch")
Set Formula = relations.CreateFormula("Formula.2", "", Helix.Height, "HelixHeight")
' ' Generating Steps
' --- Dim RefLine As Object
Dim RefPlane As Object
Dim RefHelix As Object
Dim RefPlaneOffset As Object
Dim Pt0 As Object
Dim Pt1 As Object
Dim Pt2 As Object
Dim Pt3 As Object
Dim LinePt0Pt1 As Object
Dim LinePt0Pt2 As Object
Dim RefFill As Object
Dim RefExtrude As Object
' Compute number of step to generate Dim indice As Integer
indice = HelixHeight / HauteurMarche ' Starting plane for helix/steps Dim PlaneOffset1 As Object
Set PlaneOffset1 = PartDoc.Part.HybridShapeFactory.AddNewPlaneOffset(Plane, 0, False) myHBody.AppendHybridShape PlaneOffset1
' Setting reference objet use for each step
' Note: RefPlane is the basic plane used for each step / it is updated in the loop Set RefLine = PartDoc.Part.CreateReferenceFromObject(Line)
Set RefPlane = PartDoc.Part.CreateReferenceFromObject(PlaneOffset1)
Set RefHelix = PartDoc.Part.CreateReferenceFromObject(Helix) '
' Loop on steps
' '
Dim CounterStep As Integer
For CounterStep = 1 To indice Step 1
' Create a new openbody
Set myHBody = PartDoc.Part.HybridBodies.Add()
'Point0 = Point reference for the step on axis Dim Intersection1 As Object
Set Intersection1 = PartDoc.Part.HybridShapeFactory.AddNewIntersection(RefLine, RefPlane) myHBody.AppendHybridShape Intersection1
Set Pt0 = PartDoc.Part.CreateReferenceFromObject(Intersection1) PartDoc.Part.HybridShapeFactory.GSMVisibility Pt0, 0
'Point1 = Point reference for the step on helix Dim Intersection2 As Object
Set Intersection2 = PartDoc.Part.HybridShapeFactory.AddNewIntersection(RefPlane, RefHelix) myHBody.AppendHybridShape Intersection2
PartDoc.Part.HybridShapeFactory.GSMVisibility Pt1, 0
'PlanOffset= Step height reference plane Dim PlaneOffset2 As Object
Set PlaneOffset2 = PartDoc.Part.HybridShapeFactory.AddNewPlaneOffset(RefPlane, HauteurMarche, False)
myHBody.AppendHybridShape PlaneOffset2
Set Formula = relations.CreateFormula("Formula.Step.1", "", PlaneOffset2.Offset, "StepHeight")
Set RefPlaneOffset = PartDoc.Part.CreateReferenceFromObject(PlaneOffset2) PartDoc.Part.HybridShapeFactory.GSMVisibility RefPlaneOffset, 0
'Point3 = Point reference on helix Dim Intersection3 As Object
Set Intersection3 = PartDoc.Part.HybridShapeFactory.AddNewIntersection(RefPlaneOffset, RefHelix)
myHBody.AppendHybridShape Intersection3
Set Pt3 = PartDoc.Part.CreateReferenceFromObject(Intersection3) PartDoc.Part.HybridShapeFactory.GSMVisibility Pt3, 0
'Point2 = Point Projected from helix on step ground plane Dim Project1 As Object
Set Project1 = PartDoc.Part.HybridShapeFactory.AddNewProject(Pt3, RefPlane) Project1.SolutionType = 0 Project1.Normal = True myHBody.AppendHybridShape Project1 Set Pt2 = PartDoc.Part.CreateReferenceFromObject(Project1) PartDoc.Part.HybridShapeFactory.GSMVisibility Pt2, 0
' Step definition contours : 2 lines and a circle arc ' Line1
Set LinePt0Pt1 = PartDoc.Part.HybridShapeFactory.AddNewLinePtPt(Pt0, Pt1) myHBody.AppendHybridShape LinePt0Pt1
Dim RefLinePt0Pt1 As Object
Set RefLinePt0Pt1 = PartDoc.Part.CreateReferenceFromObject(LinePt0Pt1) PartDoc.Part.HybridShapeFactory.GSMVisibility RefLinePt0Pt1, 0
' Line2
Set LinePt0Pt2 = PartDoc.Part.HybridShapeFactory.AddNewLinePtPt(Pt0, Pt2) myHBody.AppendHybridShape LinePt0Pt2
Dim RefLinePt0Pt2 As Object
Set RefLinePt0Pt2 = PartDoc.Part.CreateReferenceFromObject(LinePt0Pt2)
' circle arc
Dim Circle3Points As Object
Set Circle3Points = PartDoc.Part.HybridShapeFactory.AddNewCircle3Points(Pt0, Pt1, Pt2) Circle3Points.SetLimitation 2
myHBody.AppendHybridShape Circle3Points
Dim RefCircle As Object
Set RefCircle = PartDoc.Part.CreateReferenceFromObject(Circle3Points)
Dim Split As Object
Set Split = PartDoc.Part.HybridShapeFactory.AddNewHybridSplit(RefCircle, RefLinePt0Pt1, 1) PartDoc.Part.HybridShapeFactory.GSMVisibility RefLinePt0Pt1, 0
PartDoc.Part.HybridShapeFactory.GSMVisibility RefLinePt0Pt2, 0 PartDoc.Part.HybridShapeFactory.GSMVisibility RefCircle, 0 myHBody.AppendHybridShape Split
Dim RefSplit As Object
Set RefSplit = PartDoc.Part.CreateReferenceFromObject(Split)
Dim Fill As Object
Dim FillEdge1 As Object
Dim FillEdge2 As Object
Dim FillEdge3 As Object
Set Fill = PartDoc.Part.HybridShapeFactory.AddNewFill()
Set FillEdge1 = PartDoc.Part.HybridShapeFactory.AddNewFillEdge(RefLinePt0Pt1, 0, 0) Fill.AddFillEdge FillEdge1
Set FillEdge2 = PartDoc.Part.HybridShapeFactory.AddNewFillEdge(RefSplit, 0, 0) Fill.AddFillEdge FillEdge2
Set FillEdge3 = PartDoc.Part.HybridShapeFactory.AddNewFillEdge(RefLinePt0Pt2, 0, 0) Fill.AddFillEdge FillEdge3
myHBody.AppendHybridShape Fill
'opposite-step surface Dim Extrude As Object
Set Extrude = PartDoc.Part.HybridShapeFactory.AddNewExtrude(RefLinePt0Pt2, HauteurMarche, 0, Dir)
myHBody.AppendHybridShape Extrude
Set Formula = relations.CreateFormula("Formula.Step.2", "", Extrude.BeginOffset, "StepHeight")
' Join of two surfaces
Set RefFill = PartDoc.Part.CreateReferenceFromObject(Fill)
Set RefExtrude = PartDoc.Part.CreateReferenceFromObject(Extrude)
Dim Join As Object
Set Join = PartDoc.Part.HybridShapeFactory.AddNewJoin(RefFill, RefExtrude) PartDoc.Part.HybridShapeFactory.GSMVisibility RefFill, 0
PartDoc.Part.HybridShapeFactory.GSMVisibility RefExtrude, 0 myHBody.AppendHybridShape Join
' End of loop - re-init ref plane for next step ' RefPlane = RefPlaneOffset
Set RefPlane = PartDoc.Part.CreateReferenceFromObject(PlaneOffset2)
Next
'Model update
' Note : Performed only at the end of geometry generation PartDoc.Part.Update
' Reframing CATIA Part Window
Dim specsAndGeomWindow1 As Window
Set specsAndGeomWindow1 = CATIA.ActiveWindow
Dim viewer3D1 As Viewer
Set viewer3D1 = specsAndGeomWindow1.ActiveViewer viewer3D1.Reframe
Dim viewpoint3D1 As Viewpoint3D
Set viewpoint3D1 = viewer3D1.Viewpoint3D
End Sub
Design
them into datum
This macro shows how to create wireframe and shape feature / convert to datum of
corresponding dimension / delete features /change properties of features in a
CATPart document.
The macro opens a CATIA Part Document CAAGsiStart.CATPart
Note:
- The resulting document can be saved by setting the CAA_GSD_SAVE runtime
environment variable
- Moreover, if CAA_GSD_EXIT variable is setted, the macro exit from CATIA
CAAGsiCreatePtLnAndConvertToDatum is launched in CATIA [1]. No open document is
needed.
CAAGsiCreatePtLnAndConvertToDatum.CATScript is located in the
CAAScdGsiUseCases module. Execute macro (Windows TM only).
CAAGsiCreatePtLnAndConvertToDatum includes five steps:
1.
Openning CATPart Document and retrieving currentOpenBody
2.
Creating associative points and lines
3.
Converting middle points and lines into datum
4.
Deleting useless remaining points
5.
Changing graphic properties(color) and datum names
6.
Saving the CATPart Document and exiting CATIA
Openning CATIA Part Document and retrieving OpenBody
' Open CATIA Part : CAAGsiCreateJoinSurface.CATPart Dim sDocPath As String
sDocPath=CATIA.SystemService.Environ("CATDocView")
Dim partDocument1 As Document
Set partDocument1 = documents1.Open(sDocPath & "\online\CAAScdGsiUseCases\samples\CAAGsiStart.CATPart")
Dim part1 As Part
Set part1 = partDocument1.Part ' Retrieving the active OpenBody Dim hybridShapeFactory1 As Factory
Set hybridShapeFactory1 = part1.HybridShapeFactory
Dim hybridBodies1 As HybridBodies
Set hybridBodies1 = part1.HybridBodies
Dim hybridBody1 As HybridBody
Set hybridBody1 = hybridBodies1.Item("Open_body.1")
Opens the starting CATIA Part document and retieves OpenBody , the document
contains parameters in order to store the number of created iterations.
Creating associative points and lines
The VBScript macro use the "max" internal parameter to define the number of
iterations
' Array ' ---Dim TabExt () Dim TabMil () Dim TabLine() Dim TabLineExpl() Dim TabPtExpl() ReDim TabExt(2*max) ReDim TabMil(max) ReDim TabLine(max) ReDim TabLineExpl(max) ReDim TabPtExpl(max)Defines VBScript arrays for keeping generated hybridshapes objects in order to
access them in following steps
Dim Pi As double
Dim R As double
Dim Omega As double R = 50.0000
Pi = 3.14116 Omega= 2*Pi/max
Defines some parameters for creating point and line.
Note : In the sample lines form a sort of "hyperboloid" 3D form:
Extremities of lines are defines on two mathematical computed circles position of
point are taken a with 2*Pi/3 phase.
' ---' GSD Geometrie Creation
'
---Catia.SystemService.Print "(CAAGsiCreatePtLnAndConvertToDatum) Create Points and Lines " for i=1 to max
'Create two points Angle = Omega * (i-1)
Set TabExt(2*i-1) = hybridShapeFactory1.AddNewPointCoord(R*cos(Angle), R*sin(Angle), 100.000000)
hybridBody1.AppendHybridShape TabExt(2*i-1) part1.InWorkObject = TabExt(2*i-1)
Set TabExt(2*i) = hybridShapeFactory1.AddNewPointCoord(R*cos(Angle+2*Pi/2), R*sin(Angle+2*Pi/2), -100.000000)
hybridBody1.AppendHybridShape TabExt(2*i) part1.InWorkObject = TabExt(2*i)
'Draw line
Set reference1 = part1.CreateReferenceFromObject(TabExt(2*i-1))
Set reference2 = part1.CreateReferenceFromObject(TabExt(2*i))
Set TabLine(i) = hybridShapeFactory1.AddNewLinePtPt(reference1, reference2) hybridBody1.AppendHybridShape TabLine(i)
part1.InWorkObject = TabLine(i)
'Generate Intersection Point
Set reference3 = part1.CreateReferenceFromObject(TabLine(i))
Set originElements1 = part1.OriginElements
Set hybridShapePlaneExplicit1 = originElements1.PlaneXY
Set reference4 = part1.CreateReferenceFromObject(hybridShapePlaneExplicit1)
Set TabMil(i) = hybridShapeFactory1.AddNewIntersection(reference3, reference4) hybridBody1.AppendHybridShape TabMil(i)
part1.InWorkObject = TabMil(i)
'Settings status parameter : Num_Of_Points_Created parameter intParam1.Value = i
'Settings status parameter : Percentage_Completed parameter realParam1.Value = i/max *100
next
part1.Update
Creates extremity points / lines / middle points of lines and keep in dedicated arrays
Converting middle point and lines into datum
---' Convert to Datum
'
---' Add OpenBodys for datum point and for datum line Dim OpenBody1 As HybridBody
Dim OpenBody2 As HybridBody
Dim referencebody As Reference
Set OpenBody1 = hybridBodies1.Add()
Set referencebody = part1.CreateReferenceFromObject(OpenBody1)
hybridShapeFactory1.ChangeFeatureName referencebody , "DatumPointBody"
Set OpenBody2 = hybridBodies1.Add()
Set referencebody = part1.CreateReferenceFromObject(OpenBody2)
hybridShapeFactory1.ChangeFeatureName referencebody , "DatumLineBody" ' Loop on element to convert
for i=1 to max
'Isolate Intersection point
Set reference5 = part1.CreateReferenceFromObject(TabMil(i))
Set TabPtExpl(i) = hybridShapeFactory1.AddNewPointDatum(reference5) OpenBody1.AppendHybridShape TabPtExpl(i)
part1.InWorkObject = TabPtExpl(i)
hybridShapeFactory1.DeleteObjectForDatum reference5 'Isolate the line
Set reference5 = part1.CreateReferenceFromObject(TabLine(i))
Set TabLineExpl(i) = hybridShapeFactory1.AddNewLineDatum(reference5) OpenBody2.AppendHybridShape TabLineExpl(i) part1.InWorkObject = TabLineExpl(i) hybridShapeFactory1.DeleteObjectForDatum reference5 next part1.Update
Point datum and Line datum are stored respectively in an OpenBody for PointDatum
an one for LineDatum
Deleting useless features
' ---' Delete Useless points
' ---for i=1 to max
selection1.Clear() selection1.Add(TabExt(2*i-1)) selection1.Add(TabExt(2*i)) selection1.Delete next part1.Update
Uses of selection mecanism in order to manage deletion .
All extremities of lines are removed.
Changing graphic properties(color) and datum names
---' Change graphic properties(color) and datum names '
---Dim referencedat1 As Reference
Dim referencedat2 As Reference ' Loop on element to modify for i=1 to max
' -- Points
' Change Color of Middle Point selection1.Clear()
selection1.Add(TabPtExpl(i))
Set VisPropSet1 = selection1.VisProperties
VisPropSet1.SetRealColor 255, int(255*(i-1)/max), int(255*(1-((i-1)/max)) ), 1 ' Rename
NewName ="PointDatum" & "." & i
Set referencedat1 = part1.CreateReferenceFromObject(TabPtExpl(i)) hybridShapeFactory1.ChangeFeatureName referencedat1 ,NewName ' -- Lines
' Change Color of Line selection1.Clear()
selection1.Add(TabLineExpl(i))
Set VisPropSet1 = selection1.VisProperties
VisPropSet1.SetRealColor int(255*(i-1)/max), 255, int(255*(1-((i-1)/max)) ), 1
' Rename
NewName = "LineDatum" & "." & i
Set referencedat2 = part1.CreateReferenceFromObject(TabLineExpl(i)) hybridShapeFactory1.ChangeFeatureName referencedat2 ,NewName
next
Uses of visualisation method for setting color of an object (R,V,B) with each from 0 to
255
Uses of ChangeFeatureName method implemented in HybridshapeFactory for
renaming Feature
Important note : The availability of this method is temporary proposed: a more
general way for renaming features will be provided in future releases.
Saving the CATPart Document and exiting CATIA
On Error Resume Next
CATIA.DisplayFileAlerts = False
' ---' Save As
'
---' Note : Optional - allows to specify where document should be saved Dim sTmpPath As String
sTmpPath=CATIA.SystemService.Environ("CATTemp")
If (Not CATIA.FileSystem.FolderExists(sTmpPath)) Then
Err.Raise 9999,,"No Tmp Path Defined"
End If ' Save
partDocument1.SaveAs sTmpPath & "\CAAGsiCreatePtLnAndConvertToDatum.CATPart" '
---' Close and Quit
' ---partDocument1.Close
Catia.Quit
Allow to store document in a user choosen directory and name
Note: The number of part update is optimized all along the VBScript macro
It allows to save performances in replaying macro
Create Open Bodies
Option Explicit' COPYRIGTH DASSAULT SYSTEMES 2000 Dim Language as String
Language="VBScript"
' *********************************************************************** ' Purpose: Create a Open Bodies
' Assumtions: ' Author: ' Languages: VBScript ' Locales: English ' CATIA Level: V5R6 ' *********************************************************************** Sub CATMain()
' Creating a Part Document Dim PartDocument1 As Document
Set PartDocument1 = CATIA.Documents.Add ( "Part" )
' Retrieving a Part HybridBodies collection for attaching OpenBodies Dim hybridBodies1 As HybridBodies
Set hybridBodies1 = PartDocument1.Part.HybridBodies ' Add an first OpenBody
Dim OpenBody1 As HybridBody
Set OpenBody1 = hybridBodies1.Add() ' Add a second OpenBody
Dim OpenBody2 As HybridBody
Set OpenBody2 = hybridBodies1.Add() ' Updating CATIA PArt
PartDocument1.Part.Update
Create a Join surface
Option Explicit' COPYRIGHT DASSAULT SYSTEMES 2000 Dim Language as String
Language="VBScript"
' *********************************************************************** ' Purpose: Create a Join surface
' Assumtions: .\samples\CAAGsiCreateJoinSurface.CATPart use as input geometry ' Author: ' Languages: VBScript ' Locales: English ' CATIA Level: V5R6 ' *********************************************************************** Sub CATMain()
' Openning CATIA Part : CAAGsiCreateJoinSurface.CATPart Dim sDocPath As String
sDocPath=CATIA.SystemService.Environ("CATDocView")
Dim oPartDocument As PartDocument
Set oPartDocument = CATIA.Documents.Open(sDocPath &
"\online\CAAScdGsiUseCases\samples\CAAGsiCreateJoinSurface.CATPart") ' Retrieving Active OpenBody
Dim hybridBodies1 As HybridBodies
Set hybridBodies1 = oPartDocument.Part.HybridBodies
Dim OpenBody1 As HybridBody
Set OpenBody1 = hybridBodies1.Item("Open_body.2") ' Creating a reference for Fill.1 object
Dim hybridShapeFill1 As HybridShape
Set hybridShapeFill1 = OpenBody1.HybridShapes.Item("Fill.1")
Dim reference1 As Reference
Set reference1 = oPartDocument.Part.CreateReferenceFromObject(hybridShapeFill1) ' Creating a reference for Extrude.1 object
Dim hybridShapeExtrude1 As HybridShape
Set hybridShapeExtrude1 = OpenBody1.HybridShapes.Item("Extrude.1")
Dim reference2 As Reference
Set reference2 = oPartDocument.Part.CreateReferenceFromObject(hybridShapeExtrude1) ' Retrieving Wireframe and Shape Design Factory
Dim hybridShapeFactory1 As Factory
Set hybridShapeFactory1 = oPartDocument.Part.HybridShapeFactory ' Creating a Join (also named assemble) between Fill.1 and Extrude.1
Dim hybridShapeAssemble1 As HybridShapeAssemble
Set hybridShapeAssemble1 = hybridShapeFactory1.AddNewJoin(reference1, reference2) ' Inserting Join in current OpenBody
OpenBody1.AppendHybridShape hybridShapeAssemble1 ' Making Join as active object in CATIA Part
oPartDocument.Part.InWorkObject = hybridShapeAssemble1 ' Updating CATPart
oPartDocument.Part.Update
End Sub
Generative Shape Design
Creating an helicoidal Stair
This macro shows how to use Knowledge parameters and Generative Shape Design to
create repetitive geometry in a CATIA Macro:
The macro illustrates the following topics :
•
Creating a CATIA Part document.
•
Show/Noshow of wireframe and surfacic objects.
•
Adding a new OpenBody.
•
Creating parameters and formulas / Using them in wireframe and surface object
definitions.
•
Generating reference for each input object / CATIA methods require references as
input for object creations.
•
Creating wireframe and shape design objects.
•
Updating CATIA Part/ Note: it has to be done at the end for better performances
when replaying macro.
•
Reframing the 3D window.
CAAGsiCreateStair is launched in CATIA [1]. Open a CATIA Part Document.
CAAGsiCreateStair.CATScript is located in the CAAScdGsiUseCases module. Execute macro
(windows only).
CAAGsiCreateStair includes four steps
1.
Creating a CATIA
Part Document
2.
Settting
variables and knowledge parameters
3.
Computing the number of step for the stair
4.
Generating steps
5.
Updating CATIA Part Document and reframing
Creating a CATIA Part Document.
' Creating a Part Document Dim PartDoc As Document
Set PartDoc = CATIA.Documents.Add ( "Part" )
Setting variables and knowledge parameters.
' Setting knowledge parameters Dim parameters As Object
Set parameters = PartDoc.Part.parameters ' declare of a working Parm object
Dim Parm As Object
' Setting knowledge relations Dim relations As Object
Set relations = PartDoc.Part.relations ' declare a working Formula object Dim Formula As Object
The "Parm" and "Formula" objects are collections of knowledge parameters and
Formula.
' Creating Parameters for stair generation
Set Parm = parameters.CreateDimension("HelixPitch", "LENGTH", Pitch)
Set Parm = parameters.CreateDimension("HelixHeight", "LENGTH", Height)
Set Parm = parameters.CreateDimension("StepHeight", "LENGTH", StepValue)
The definition of knowledge parameters and their use in creating objects allows to
parameterize object creation.
That is a key point in using CATIA V5 objects.
For example, if the "StepHeight" is not correct in the resulting CATIA Part , it can be
interactively modified
and all the objects using "StepHeight" value are automatically updated.
' Creating a line for helix definitionDim Line As Object
Set Line = PartDoc.Part.HybridShapeFactory.AddNewLinePtDir(Point1, Dir, 0, HelixHeight, False) myHBody.AppendHybridShape Line
' Create a formula defining the line offset value as equal to the helix height parameter Set Formula = relations.CreateFormula("Formula.0", "", Line.EndOffset, "HelixHeight") ' Set Parameters for stair generation
Set Parm = parameters.CreateDimension("HelixPitch", "LENGTH", Pitch)
Set Parm = parameters.CreateDimension("HelixHeight", "LENGTH", Height)
Set Parm = parameters.CreateDimension("StepHeight", "LENGTH", StepValue)
The defined parameters can be used to create a formula.
For example, a formula can be associated with the Line object parameter in order to be
updated if the "HelixHeight" paramater is modified
Computing the number of steps for the stairs
' Computing the number of steps to be generated Dim index As Integer
index = HelixHeight / StepHeight ...
' ' Loop on steps
' '
Dim CounterStep As Integer
For CounterStep = 1 To index Step 1
...
Next
The number of steps is directly related to the helix height and the step height.
The number of step and then of the objects created in the CATIA Part Document depend on
these two parameters
' Create a new openbody
Set myHBody = PartDoc.Part.HybridBodies.Add()
Each geometry related to one step is inserted in a specific OpenBody
'Point0 = Point reference for the step on the axisDim Intersection1 As Object
Set Intersection1 = PartDoc.Part.HybridShapeFactory.AddNewIntersection(RefLine, RefPlane) myHBody.AppendHybridShape Intersection1
Set Pt0 = PartDoc.Part.CreateReferenceFromObject(Intersection1) PartDoc.Part.HybridShapeFactory.GSMVisibility Pt0, 0
'Point1 = Point reference for the step on the helix Dim Intersection2 As Object
Set Intersection2 = PartDoc.Part.HybridShapeFactory.AddNewIntersection(RefPlane, RefHelix) myHBody.AppendHybridShape Intersection2
Set Pt1 = PartDoc.Part.CreateReferenceFromObject(Intersection2) PartDoc.Part.HybridShapeFactory.GSMVisibility Pt1, 0
'PlanOffset= Step height reference plane Dim PlaneOffset2 As Object
Set PlaneOffset2 = PartDoc.Part.HybridShapeFactory.AddNewPlaneOffset(RefPlane, StepHeight, False)
myHBody.AppendHybridShape PlaneOffset2
Set Formula = relations.CreateFormula("Formula.Step.1", "", PlaneOffset2.Offset, "StepHeight")
Set RefPlaneOffset = PartDoc.Part.CreateReferenceFromObject(PlaneOffset2) PartDoc.Part.HybridShapeFactory.GSMVisibility RefPlaneOffset, 0
A formula is associated with the offset value of the "PlaneOffset" from the "RefPlane". Then,
the "StepHeight" parameter pilots
the object creation and will update the offsetted plane position if its value is modified
afterwards.
'Point3 = Point reference on the helix Dim Intersection3 As Object
Set Intersection3 = PartDoc.Part.HybridShapeFactory.AddNewIntersection(RefPlaneOffset, RefHelix)
myHBody.AppendHybridShape Intersection3
Set Pt3 = PartDoc.Part.CreateReferenceFromObject(Intersection3) PartDoc.Part.HybridShapeFactory.GSMVisibility Pt3, 0
'Point2 = Point Projected from the helix on the step ground plane Dim Project1 As Object
Set Project1 = PartDoc.Part.HybridShapeFactory.AddNewProject(Pt3, RefPlane) Project1.SolutionType = 0 Project1.Normal = True myHBody.AppendHybridShape Project1 Set Pt2 = PartDoc.Part.CreateReferenceFromObject(Project1) PartDoc.Part.HybridShapeFactory.GSMVisibility Pt2, 0
' Step definition contours : 2 lines and a circle arc ' Line1
Set LinePt0Pt1 = PartDoc.Part.HybridShapeFactory.AddNewLinePtPt(Pt0, Pt1) myHBody.AppendHybridShape LinePt0Pt1
Dim RefLinePt0Pt1 As Object
Set RefLinePt0Pt1 = PartDoc.Part.CreateReferenceFromObject(LinePt0Pt1) PartDoc.Part.HybridShapeFactory.GSMVisibility RefLinePt0Pt1, 0
' Line2
Set LinePt0Pt2 = PartDoc.Part.HybridShapeFactory.AddNewLinePtPt(Pt0, Pt2) myHBody.AppendHybridShape LinePt0Pt2
Dim RefLinePt0Pt2 As Object
Set RefLinePt0Pt2 = PartDoc.Part.CreateReferenceFromObject(LinePt0Pt2)
' Circle arc
Dim Circle3Points As Object
Set Circle3Points = PartDoc.Part.HybridShapeFactory.AddNewCircle3Points(Pt0, Pt1, Pt2) Circle3Points.SetLimitation 2
myHBody.AppendHybridShape Circle3Points
Dim RefCircle As Object
Set RefCircle = PartDoc.Part.CreateReferenceFromObject(Circle3Points)
Dim Split As Object
Set Split = PartDoc.Part.HybridShapeFactory.AddNewHybridSplit(RefCircle, RefLinePt0Pt1, 1) PartDoc.Part.HybridShapeFactory.GSMVisibility RefLinePt0Pt1, 0
PartDoc.Part.HybridShapeFactory.GSMVisibility RefLinePt0Pt2, 0 PartDoc.Part.HybridShapeFactory.GSMVisibility RefCircle, 0 myHBody.AppendHybridShape Split
Dim RefSplit As Object
Set RefSplit = PartDoc.Part.CreateReferenceFromObject(Split)
' Step surface Dim Fill As Object
Dim FillEdge1 As Object
Dim FillEdge2 As Object
Set Fill = PartDoc.Part.HybridShapeFactory.AddNewFill()
Set FillEdge1 = PartDoc.Part.HybridShapeFactory.AddNewFillEdge(RefLinePt0Pt1, 0, 0) Fill.AddFillEdge FillEdge1
Set FillEdge2 = PartDoc.Part.HybridShapeFactory.AddNewFillEdge(RefSplit, 0, 0) Fill.AddFillEdge FillEdge2
Set FillEdge3 = PartDoc.Part.HybridShapeFactory.AddNewFillEdge(RefLinePt0Pt2, 0, 0) Fill.AddFillEdge FillEdge3
myHBody.AppendHybridShape Fill
' Rising the opposite surface step Dim Extrude As Object
Set Extrude = PartDoc.Part.HybridShapeFactory.AddNewExtrude(RefLinePt0Pt2, HauteurMarche, 0, Dir)
myHBody.AppendHybridShape Extrude
Set Formula = relations.CreateFormula("Formula.Step.2", "", Extrude.BeginOffset, "StepHeight")
' Joining the two surfaces
Set RefFill = PartDoc.Part.CreateReferenceFromObject(Fill)
Set RefExtrude = PartDoc.Part.CreateReferenceFromObject(Extrude)
Dim Join As Object
Set Join = PartDoc.Part.HybridShapeFactory.AddNewJoin(RefFill, RefExtrude) PartDoc.Part.HybridShapeFactory.GSMVisibility RefFill, 0
PartDoc.Part.HybridShapeFactory.GSMVisibility RefExtrude, 0 myHBody.AppendHybridShape Join
All the objects necessary to create one step are generated
Note :The wireframe and shape design objects can be put in no-show using the specific
method of HybridShapeFactory: GSMVisibility
' End of loop - re-initializing the reference plane for the next step ' RefPlane = RefPlaneOffset
Set RefPlane = PartDoc.Part.CreateReferenceFromObject(PlaneOffset2)
For the next loop, the "RefPlane" is updated.
The new "RefPlane" for the next step is the OffsetPane used in current step
Updating CATIA Part Document and reframing
'Updating CATIA Part Document
' Note : Performed only at the end of geometry generation PtDoc.Part.Update
Note: The update of the CATIA Part Document is done at the end for performances reason :
The generation of the geometry representation of the objects and their visualization is done
in one step
' Reframing CATIA Part Window
Dim specsAndGeomWindow1 As Window
Set specsAndGeomWindow1 = CATIA.ActiveWindow
Dim viewer3D1 As Viewer
Set viewer3D1 = specsAndGeomWindow1.ActiveViewer viewer3D1.Reframe
Dim viewpoint3D1 As Viewpoint3D
The reframing is done to view in the 3D window all objects created and updated once the
macro has been replayed
This macro shows how to create geometry from existing geometry in a CATPart
document.
The macro opens a CATIA Part Document and creates a Join surface using pre-existing
geometry (Fill and Extrude).
CAAGsiCreateJoinSurface is launched in CATIA [1]. No open document is needed.
CAAGsiCreateJoinSurface.CATScript is located in the CAAScdGsiUseCases module.
Execute macro (Windows TM only).
CAAGsiCreateJoinSurface includes five steps:
1.
Openning
CATPart Document
2.
Retrieving the current open body
3.
Creating references for objects used as input for the join
4.
Creating the join
5.
Setting the created join as the current working object
6.
Updating The CATPart Document
Openning CATIA Part Document
' Opening a CATIA Part document : CAAGsiCreateJoinSurface.CATPart Dim sDocPath As String
sDocPath=CATIA.SystemService.Environ("CATDocView")
Dim oPartDocument As PartDocument
Set oPartDocument = CATIA.Documents.Open(sDocPath &
"\online\CAAScdGsiUseCases\samples\CAAGsiCreateJoinSurface.CATPart")
surface objects (In this test case a join).
Retrieving OpenBody
' Retrieving the active OpenBody Dim hybridBodies1 As HybridBodies
Set hybridBodies1 = oPartDocument.Part.HybridBodies
Dim OpenBody1 As HybridBody
Set OpenBody1 = hybridBodies1.Item("Open_body.2")
Retrieves the OpenBody containing initial objects. It will be re-used for creating the
Join
Creating references for objects used as input for the join
' Creating a reference for the Fill.1 object Dim hybridShapeFill1 As HybridShape
Set hybridShapeFill1 = OpenBody1.HybridShapes.Item("Fill.1")
Dim reference1 As Reference
Set reference1 = oPartDocument.Part.CreateReferenceFromObject(hybridShapeFill1) ' Creating a reference for the Extrude.1 object
Dim hybridShapeExtrude1 As HybridShape
Set hybridShapeExtrude1 = OpenBody1.HybridShapes.Item("Extrude.1")
The Fill and the Extrude surfaces used as input for the Join, are converted into
reference.
This operation is required in order to use the objects as input for the Join, all objects
used as input in IDL method interfaces are to be converted as references and passed
in creation methods .
Creating the Join
' Retrieving Wireframe and Shape Design Factory Dim hybridShapeFactory1 As Factory
Set hybridShapeFactory1 = oPartDocument.Part.HybridShapeFactory ' Creating a Join (also named assemble) between Fill.1 and Extrude.1 Dim hybridShapeAssemble1 As HybridShapeAssemble
Set hybridShapeAssemble1 = hybridShapeFactory1.AddNewJoin(reference1, reference2) ' Inserting the join in the current OpenBody
OpenBody1.AppendHybridShape hybridShapeAssemble1
The Method AddNewJoin is a method of the HybridShapeFactory IDL Interface
Once created, the join has to be inserted in an OpenBody. In the test case, the
OpenBody containing the input geometry is re-used. .
Note : A new OpenBody should have been created in order to insert the join in a
separate OpenBody.
Setting the created Join as the current working object
' Making Join as active object in CATIA Part
Updating the Part Document
' Updating CATIA Part Document oPartDocument.Part.Update
The Part has to be updated to generate the geometrical representation of the
created objects.
Once this done , the updated objects are visible in the 3D window and in the
specification tree
Note: It is recommended to launch the Part update once all new objects have been
created and inserted in an OpenBody
Language="VBSCRIPT"
Sub CATMain()
Dim documents1 As Documents
Set documents1 = CATIA.Documents ' Open CATPart
'
---Catia.SystemService.Print "(CAAGsiCreatePtLnAndConvertToDatum) Open CAAGsiStart.CATPart "
' Open CATIA Part : CAAGsiCreateJoinSurface.CATPart Dim sDocPath As String
sDocPath=CATIA.SystemService.Environ("CATDocView")
Catia.SystemService.Print "(CAAGsiCreatePtLnAndConvertToDatum) DocPath = "&sDocPath
Dim partDocument1 As Document
Set partDocument1 = documents1.Open(sDocPath & "\online\CAAScdGsiUseCases\samples\CAAGsiStart.CATPart")
Dim part1 As Part
Set part1 = partDocument1.Part
Dim hybridShapeFactory1 As Factory
Set hybridShapeFactory1 = part1.HybridShapeFactory
Dim hybridBodies1 As HybridBodies
Set hybridBodies1 = part1.HybridBodies
Dim hybridBody1 As HybridBody
Set hybridBody1 = hybridBodies1.Item("Open_body.1") ' Declarations
' ---Dim max as integer
max=20
Dim hybridShapePointCoord1 As HybridShapePointCoord
Dim hybridShapePointCoord2 As HybridShapePointCoord
Dim reference1 As Reference
Dim reference2 As Reference
Dim reference3 As Reference
Dim originElements1 As OriginElements
Dim hybridShapePlaneExplicit1 As AnyObject
Dim reference4 As Reference
Dim hybridShapeIntersection1 As HybridShapeIntersection
Dim reference5 As Reference
Dim hybridShapePointExplicit1 As HybridShapePointExplicit
Dim VisPropSet1 As VisPropertySet
Dim hybridShapeLineExplicit1 As HybridShapeLineExplicit
Dim selection1
Dim parameters1 As Parameters
Dim intParam1 As Parameter
Dim parameters2 As Parameters
Dim realParam1 As Parameter
Set selection1 = CATIA.ActiveDocument.Selection
Set parameters1 = part1.Parameters
Set intParam1 = parameters1.Item("Num_Of_Points_Created") intParam1.Value = 0
Set parameters2 = part1.Parameters
Set realParam1 = parameters2.Item("Percentage_Completed") realParam1.Value = 0.00 ' Array ' ---Dim TabExt () Dim TabMil () Dim TabLine() Dim TabLineExpl() Dim TabPtExpl() ReDim TabExt(2*max) ReDim TabMil(max) ReDim TabLine(max) ReDim TabLineExpl(max) ReDim TabPtExpl(max) Dim Pi As double Dim R As double
Dim Omega As double R = 50.0000
Pi = 3.14116 Omega= 2*Pi/max
Dim i as integer
Dim angle As double
---' GSD Geometrie Creation
'
---Catia.SystemService.Print "(CAAGsiCreatePtLnAndConvertToDatum) Create Points and Lines " for i=1 to max
'Create two points Angle = Omega * (i-1)
Set TabExt(2*i-1) = hybridShapeFactory1.AddNewPointCoord(R*cos(Angle), R*sin(Angle), 100.000000)
hybridBody1.AppendHybridShape TabExt(2*i-1) part1.InWorkObject = TabExt(2*i-1)
Set TabExt(2*i) = hybridShapeFactory1.AddNewPointCoord(R*cos(Angle+2*Pi/3), R*sin(Angle+2*Pi/3), -100.000000)
hybridBody1.AppendHybridShape TabExt(2*i) part1.InWorkObject = TabExt(2*i)
'Draw line
Set reference1 = part1.CreateReferenceFromObject(TabExt(2*i-1))
Set reference2 = part1.CreateReferenceFromObject(TabExt(2*i))
Set TabLine(i) = hybridShapeFactory1.AddNewLinePtPt(reference1, reference2) hybridBody1.AppendHybridShape TabLine(i)
part1.InWorkObject = TabLine(i)
'Generate Intersection Point
Set reference3 = part1.CreateReferenceFromObject(TabLine(i))
Set originElements1 = part1.OriginElements
Set hybridShapePlaneExplicit1 = originElements1.PlaneXY
Set reference4 = part1.CreateReferenceFromObject(hybridShapePlaneExplicit1)
Set TabMil(i) = hybridShapeFactory1.AddNewIntersection(reference3, reference4) hybridBody1.AppendHybridShape TabMil(i)
part1.InWorkObject = TabMil(i)
'update Num_Of_Points_Created parameter intParam1.Value = i
'update Percentage_Completed parameter realParam1.Value = i/max *100 next part1.Update ' ---' Convert to Datum '
---Catia.SystemService.Print "(CAAGsiCreatePtLnAndConvertToDatum) Convert to Datum "
' Add OpenBodys for datum point and for datum line Dim OpenBody1 As HybridBody
Dim OpenBody2 As HybridBody
Dim referencebody As Reference
Set OpenBody1 = hybridBodies1.Add()
hybridShapeFactory1.ChangeFeatureName referencebody , "DatumPointBody"
Set OpenBody2 = hybridBodies1.Add()
Set referencebody = part1.CreateReferenceFromObject(OpenBody2)
hybridShapeFactory1.ChangeFeatureName referencebody , "DatumLineBody" ' Loop on element to convert
for i=1 to max
'Isolate Intersection point
Set reference5 = part1.CreateReferenceFromObject(TabMil(i))
Set TabPtExpl(i) = hybridShapeFactory1.AddNewPointDatum(reference5) OpenBody1.AppendHybridShape TabPtExpl(i)
part1.InWorkObject = TabPtExpl(i)
hybridShapeFactory1.DeleteObjectForDatum reference5 'Isolate the line
Set reference5 = part1.CreateReferenceFromObject(TabLine(i))
Set TabLineExpl(i) = hybridShapeFactory1.AddNewLineDatum(reference5) OpenBody2.AppendHybridShape TabLineExpl(i) part1.InWorkObject = TabLineExpl(i) hybridShapeFactory1.DeleteObjectForDatum reference5 next part1.Update ' ---' Delete Useless points
' ---for i=1 to max
selection1.Clear() selection1.Add(TabExt(2*i-1)) selection1.Add(TabExt(2*i)) selection1.Delete next part1.Update '
---' Change graphic properties(color) and datum names '
---Catia.SystemService.Print "(CAAGsiCreatePtLnAndConvertToDatum) Update graphic properties and names "
Dim referencedat1 As Reference
Dim referencedat2 As Reference ' Loop on element to modify for i=1 to max
' -- Points
' Change Color of Middle Point selection1.Clear()
selection1.Add(TabPtExpl(i))
Set VisPropSet1 = selection1.VisProperties
VisPropSet1.SetRealColor 255, int(255*(i-1)/max), int(255*(1-((i-1)/max)) ), 1 ' Rename
NewName ="PointDatum" & "." & i
Set referencedat1 = part1.CreateReferenceFromObject(TabPtExpl(i)) hybridShapeFactory1.ChangeFeatureName referencedat1 ,NewName
' -- Lines
' Change Color of Line selection1.Clear()
selection1.Add(TabLineExpl(i))
Set VisPropSet1 = selection1.VisProperties
VisPropSet1.SetRealColor int(255*(i-1)/max), 255, int(255*(1-((i-1)/max)) ), 1
' Rename
NewName = "LineDatum" & "." & i
Set referencedat2 = part1.CreateReferenceFromObject(TabLineExpl(i)) hybridShapeFactory1.ChangeFeatureName referencedat2 ,NewName
next
EnvSave = Catia.SystemService.Environ("CAA_GSD_SAVE") Catia.SystemService.Print "EnvSave=" & EnvSave
If ( EnvSave <> "" ) Then
' ' Save As
'
' Note : Optional - allows to specify where document should be saved sTmpPath = Catia.SystemService.Environ("CATTemp")
If (Not Catia.FileSystem.FolderExists(sTmpPath)) Then
Err.Raise 9999,,"No Tmp Path Defined"
End If
' Save
Catia.SystemService.Print "(CAAGsiCreatePtLnAndConvertToDatum) SaveAs in :" & sTmpPath & "\CAAGsiCreatePtLnAndConvertToDatum.CATPart"
partDocument1.SaveAs sTmpPath & "\CAAGsiCreatePtLnAndConvertToDatum.CATPart"
End If
EnvVar = Catia.SystemService.Environ("CAA_GSD_EXIT") Catia.SystemService.Print "EnvVar=" & EnvVar
If ( EnvVar <> "" ) Then On Error Resume Next
CATIA.DisplayFileAlerts = False
' ' Close
'
Catia.SystemService.Print "(CAAGsiCreatePtLnAndConvertToDatum) Close and Quit " partDocument1.Close ' ' Exit Catia ' Catia.SystemService.Print "Catia.Quit" Catia.Quit End If End Sub
Language="VBSCRIPT"
'My macro creates five cylinders Sub CATMain()
Dim documents1 As Documents
Set documents1 = CATIA.Documents
Dim partDocument1 As Document
Set partDocument1 = documents1.Add("Part")
Dim part1 As Part
Set part1 = partDocument1.Part
Dim bodies1 As Bodies
Set bodies1 = part1.Bodies
Dim body1 As Body
Set body1 = bodies1.Item("MechanicalTool.1")
Dim sketches1 As Sketches
Set sketches1 = body1.Sketches
Dim originElements1 As OriginElements
Set originElements1 = part1.OriginElements
Dim reference1 As AnyObject
Set reference1 = originElements1.PlaneXY x = 0
Dim arrayOfVariantOfDouble1(8)
arrayOfVariantOfDouble1(0) = 0.000000 arrayOfVariantOfDouble1(1) = 0.000000 arrayOfVariantOfDouble1(2) = 0.000000