What is the CATIA structure ?
The CATIA structure is quite complex and for this tutorial, you need to open V5Automation.chm. On the first page you see this :
As the color tells it, there are three types of objects in CATIA :
- A collection : it's basically a liste of objects
- An abstract object : you can't concretely create one but you need to define them
- An object : an entity you can create it and work with it
As VBA is called an oriented-object programming language, it has a particular structure. And documents, parts, sketches, pocket, lines and points are represented as objects in VBA. we can distinct two ways for acting on objects : property and method.
Property : It's a characteristic of an object
Method : It's an action on an object
Take the example of a sketch :
For the property GeometricElements, if you've already defined a sketch named sketch1, you can type :
Property : It's a characteristic of an object
Method : It's an action on an object
Take the example of a sketch :
For the property GeometricElements, if you've already defined a sketch named sketch1, you can type :
[...] Dim geometricelements1 as GeometricElements Set geometricelements1 = sketch1.GeometricElements [...] |
Notice that Set assign an object to a variable.
For the method OpenEdition, it's :
[...] Dim factory2D1 As Factory2D Set factory2D1 = sketch1.OpenEdition [...] |
You didn't create another object with this method but just opened Sketcher.
Creating and opening a document :
Replace "Part" by "Product" or "Drawing" to have the corresponding document.
[...] Dim partdocument1 As partdocument Set pardocument1 = CATIA.Documents.Add("Part") [...] |
[...] Dim partdocument1 As partdocument Set partdocument1 = CATIA.Documents.Open("your_path\DocumentToOpen.CATPart") [...] |
Closing and saving a document :
There are three differents ways to use the Close function :
- To close the active document
[...] CATIA.ActiveDocument.Close [...] |
- To close a document assigned to a document1 variable
[...] document1.Close [...] |
- To close a document assigned to a name
[...] CATIA.Documents.Item("Name").Close [...] |
Like the close function, there are three ways to save :
To save the active document : CATIA.ActiveDocument.Save
To save a Document assigned to a document1 variable : document1.Save
To save a Document assigned to a name : CATIA.Documents.Item(“Name”).Save
In addition, you can use the very useful function SaveAs
CATIA.ActiveDocument.SaveAs “your_path\MyNewName.your_extension”
Application exercice :
Create a macro that allow ou to open the sketch edition using the CATIA structure (fonction name_of_your_sketch.OpenEdition)
Notice that you can't delete your sketch. In fact you didn't closed the editor (even if you can't see it open)
Just delete the part now and add name_of_your_sketch.CloseEdition to your macro.
First choice : the sketch is in the body of your part.
Spoiler:
Second choice : the sketch is in a Geometrical Set
Spoiler:
Before looking at the next articles I have to talk about the macro recording tool. See what the macro recording records :
Sub CATMain() Dim partDocument1 As PartDocument Set partDocument1 = CATIA.ActiveDocument Dim part1 As Part Set part1 = partDocument1.Part Dim hybridBodies1 As HybridBodies Set hybridBodies1 = part1.HybridBodies Dim hybridBody1 As HybridBody Set hybridBody1 = hybridBodies1.Item("Geometrical Set.1") Dim sketches1 As Sketches Set sketches1 = hybridBody1.HybridSketches Dim originElements1 As OriginElements Set originElements1 = part1.OriginElements Dim reference1 As Reference Set reference1 = originElements1.PlaneXY Dim sketch1 As sketch Set sketch1 = sketches1.Add(reference1) Dim arrayOfVariantOfDouble1(8) arrayOfVariantOfDouble1(0) = 0# arrayOfVariantOfDouble1(1) = 0# arrayOfVariantOfDouble1(2) = 0# arrayOfVariantOfDouble1(3) = 1# arrayOfVariantOfDouble1(4) = 0# arrayOfVariantOfDouble1(5) = 0# arrayOfVariantOfDouble1(6) = 0# arrayOfVariantOfDouble1(7) = 1# arrayOfVariantOfDouble1(8) = 0# Set sketch1Variant = sketch1 sketch1Variant.SetAbsoluteAxisData arrayOfVariantOfDouble1 part1.InWorkObject = sketch1 Dim factory2D1 As Factory2D Set factory2D1 = sketch1.OpenEdition() Dim geometricElements1 As GeometricElements Set geometricElements1 = sketch1.GeometricElements Dim axis2D1 As Axis2D Set axis2D1 = geometricElements1.Item("AbsoluteAxis") Dim line2D1 As Line2D Set line2D1 = axis2D1.GetItem("HDirection") line2D1.ReportName = 1 Dim line2D2 As Line2D Set line2D2 = axis2D1.GetItem("VDirection") line2D2.ReportName = 2 sketch1.CloseEdition part1.InWorkObject = hybridBody1 part1.Update End Sub |
There are minor differences.
First : the recorder used the command ActiveDocument. I don't recommended to use this method or whith high precaution. In fact there is a high chance that you have another document which is open in CATIA. So the macro could take another document as ActiveDocument.
It defined a safe arr : arrayOfVariantOfDouble1 You don't need them, for the moment, to have a working code. A safe array can stock variables like arrays in other programming languages. However they have to have a type (like Double, Object...) A example will be showed later.
part1.InWorkObject = sketch1 is optional
Dim factory2D1 As Factory2D
Set factory2D1 = sketch1.OpenEdition()
Factory2D is an object, which contains all the methods of Sketcher Workbench. Look at the help for the exhaustice list of methods.
The axis are automatically set so you don't need to introduce them like the macro recorder do.
The macro recorder autorename the elements with, for example, line2D1.ReportName = 1 ,but you didn't have to do it and simply use the name line2D1.
You have certainly noticed that this macro did only create a sketch and no more. The next step is to modify this sketch.
Next article : 3.Modifying a sketch
Excel doesn't recognise things like "Documents" (which is a object of Catia) so the makros cann't be executed. Could you please tell me how to solve this problem?
RépondreSupprimer