When to use CreateReference
|Many CATIA automation functions or methods require parameters of the Reference type. The reference object is designed to unify the use of both exposed in automation objects and non-exposed objects, such as b-rep elements. A reference is made if a method is used as an input parameter. A Reference represents the Reference class, which is created with the CreateReferenceFrom methods of the part object class. The reference is then passed to an object to enable associativity with the referenced object.
For example, you could use the following method to create a new sketch:
Func Add (reference iPLane) As sketch
In the above example using sketch, the new sketch in CATIA might be created on a plane (plane object) or on a planar solid face (planarface object). Because these are two very different objects one option might be to have two functions to create a new sketch – one using plane object as parameter and another using planarface object as a parameter
Instead, the CATIA programmers set one function to create a new sketch that uses a unified parameter of the reference types and then to use methods to convert objects of various types to the Reference type.
To create a new sketch on xy plane:
Dim oRefPLane As Reference Set oRefPlane = oPart.CreateReferenceFromObject(oPart.OriginElements.PlaneXY) Dim oSketch1 As Sketch Set oSketch1 = oBody.Sketches.Add(oRefPlane)
What CreateReference to Use?
There are four ways to derive a reference:
- For geometry: CreateReferenceFromGeometry
- For objects: CreateReferenceFromObject
- For names: CreateReferenceFromName (string iobjectname) As Reference
- For Brep: CreateReferenceFromBRepName (string ilabel, any object icontext) as reference
CreateReferenceFromGeometry
CreateReferenceFromGeometry derives a reference from a geometry object such as a wireframe, surface, or solid. Example:
Dim oRef As Reference Set oRef = MyPart.CreateReferenceFromGeometry(aPoint) Msgbox oRef.DisplayName
CreateReferenceFromObject
CreateReferenceFromObject is used when you have a single object pointing to another object. As an example, here’s how you would create references for objects used as inputs for a join operation:
' 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") Dim reference2 As Reference Set reference2 = oPartDocument.Part.CreateReferenceFromObject(hybridShapeExtrude1)
As you can see, the Fill and the Extrude surfaces used as input for the Join are converted into references. This operation is required in order to use the objects as input for the Join. In fact, all objects used as input in IDL method interfaces are to be converted as references and passed in creation methods.
CreateReferenceFromName
CreateReferenceFromName creates a reference to an object. This is seldom used unless you need a reference that points to nothing (if you need a reference but it won’t be determined until later).
Dim oRef As Reference Set oRef = myPart.CreateReferenceFromName(“”)
CreateReferenceFromBrep
A BREP is a boundary representation object such as a point, edge, vertex, or face of a geometric element. If a reference to part of geometry (or brep) is needed:
Dim oRef As Reference Set oRef=myPart.CreateReferenceFromBRepName(edge, pad)
DisplayName
The name of the object referenced by a reference can be determined with the Display Name property: Reference.DisplayName. It returns the name of the referenced object. The name of the referenced object is either the name displayed in the specification tree for a GeometricElement object or a character string defining the reference for a boundary object. For example, the following returns in StrName the displayable name of reference FirstRef:
StrName = FirstRef.DisplayName
Hopefully this has cleared up some of the confusion about when to use CreateReference. As always, if you have any questions please post them on the forum.
“CreateReferenceFromName creates a reference to an object. This is seldom used unless you need a reference that points to nothing”
Or You want to create a Reference inside a Product. There seems to be no other way…
Hello,
i need to read a surface of a part, after create a sketch containing 4 points on that surface, and after on one of the points I should create a hole. I have big problems with how to define the point in the “AddNewHoleFromRefPoint” command. If I record macro, it uses the CreateReferenceFromBRepName, but I don’t understand the arguments of the function.
The idea is that I want to be able to be able to put the hole on point.1 or point.2 or point.3 or point.4, but i don’t know how to do this using the code.
Please help :).
Don’t know if I made myself too clear, if needed I can provide the code I have so far.
Somebody please help me understand why is the create constraint method failing. Please see the code snippet below
Description: I have a product with two parts in it. I have to constraint a point in the geometrical set of first part to a point in the geometrical set of another part
‘ * Start of Code Snippet
‘*** relevant codes
‘
‘
‘
‘
Dim oConstraints As Constraints
Set oConstraints = CATIA.ActiveDocument.Product.Connections(“CATIAConstraints”)
Dim pointhybshape As Variant
Set pointhybshape = bhybridShapes.Item(“SnapPoint”)
Dim PointRefOnPart As Reference
Set PointRefOnPart = CorrespondingPart.CreateReferenceFromObject(pointhybshape)
Dim PointRefOnPanel As Reference
Set PointRefOnPanel = CorrespondingPanelPart.CreateReferenceFromObject(ArrayToHoldLinesPoints(j – 1, 0)) ”’The ArrayToHoldLinesPoints is an 2-D array that holds hybridshapes as follows
”Set ArrayToHoldLinesPoints(i – 1, 0) = ohybridShapes.Item(i)
Dim FirstConstraint As Constraint
Set FirstConstraint = oConstraints.AddBiEltCst(catCstTypeOn, PointRefOnPart, PointRefOnPanel) ” Code fails here. Error message is Run time error -2147467259, Method AddBiEltCst of Object Constraints Failed
‘
‘
‘
‘
‘* End of code snippet