Create Reference from Selection Object?

Paul Hamrick

Create Reference from Selection Object?
All, I'm trying to automate the creation of a symmetry of a part body. The macro prompts the user to select a plane/face and once the user clicks "OK" the macro takes the selected element and performs a symmetry. The following code is a plain-old recorded macro of a symmetry creation:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Dim partDocument1 As PartDocument
Set partDocument1 = CATIA.ActiveDocument

Dim part1 As part
Set part1 = partDocument1.part

Dim shapeFactory1 As ShapeFactory
Set shapeFactory1 = part1.ShapeFactory

Dim bodies1 As Bodies
Set bodies1 = part1.Bodies

Dim body1 As Body Set body1 = bodies1.Item("Paul")
Dim shapes1 As Shapes
Set shapes1 = body1.Shapes

Dim solid1 As Solid
Set solid1 = shapes1.Item("Solid.1")

Dim reference1 As Reference
Set reference1 = part1.CreateReferenceFromBRepName("RSurFaceBrpSolid.1;%1);None);Cf11));WithPermanentBody;WithoutBuildError;WithSelectingFeatureSupport;MFBRepVersion_CXR15)", solid1)

Dim selection1 As Selection
selection1.Item2(1).Reference

Dim symmetry1 As Symmetry
Set symmetry1 = shapeFactory1.AddNewSymmetry2(reference1)

Dim hybridShapeSymmetry1 As HybridShapeSymmetry
Set hybridShapeSymmetry1 = symmetry1.HybridShape

part1.InWorkObject = hybridShapeSymmetry1

part1.Update

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Notice that the Reference object "reference1" is Set using CreateReferenceFromBRepName. Unfortunately I cannot do this in my code (I don't think). I need to take a plane/face that is contained in a Selection and set "reference1" to it. Does anyone know how to do this? I've tried to get it done by trial-and-error but I've not gotten anything to work.

Douglas Snell

RE: Create Reference from Selection Object?
(in response to Paul Hamrick)
I believe your looking for CreateReferenceFromObject:

Dim reference1 As reference
Set reference1 = part1.CreateReferenceFromObject(solid1)

Paul Hamrick

RE: Create Reference from Selection Object?
(in response to Paul Hamrick)

I've tried this and I don't think it works.   Here are some examples:

~~~~~~~~~~ Example 1 ~~~~~~~~~~~~~~~~~~~~

'A face of a the partbody is selected and the only element of selection1


Dim planarface1 As PlanarFace

Set planarface1 = selection1.Item2(1).Value
Set reference1 = part1.CreateReferenceFromObject(planarface1 )

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

 

~~~~~~~~~~ Example 2 ~~~~~~~~~~~~~~~~~~~~

'A face of a the partbody is selected and the only element of selection1


Dim face1 As Face

Set face1 = selection1.Item2(1).Value
Set reference1 = part1.CreateReferenceFromObject(face1 )

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

 

Neither of these work for me, I get a runtime error at the "Set reference1...." line.

Douglas Snell

RE: Create Reference from Selection Object?
(in response to Paul Hamrick)

The reason your having difficulty is your mixing to different methods for creating a reference.

In your first example, you retrieved the Solid.1 object with this line:  Set solid1 = shapes1.Item("Solid.1&quot.   If you wanted to create a reference to this object you would use the CreateReferenceFromObject method. 

Now, if you are selecting geometry in the 3D window (Face, edge, vertex, etc) as you did in the second example you are already selecting a reference so you can:

set reference1 = selection1.item2(1).value

Or, if you want to use a create reference method you can use selection.item2(1).value.name (string) in conjunction with PartDoc.createreferencefrombrep().

 

Paul Hamrick

RE: Create Reference from Selection Object?
(in response to Paul Hamrick)

DOUGSNELL,

Thanks for the reply.  I'm not understanding what you are saying when you say I am mixing methods.

I'm pretty sure I've tried "set reference1 = selection1.item2(1).value" and it does not work.  Don't you use the "value" property to get the actual object in a selection?  There is a "reference" property I know, so earlier I tried

reference1 = selection1.item2(1).reference

however when reference1 was fed into the

Dim symmetry1 As Symmetry
Set symmetry1 = shapeFactory1.AddNewSymmetry2(reference1)

code the "AddNewSymmetry2" method broke (runtime error).

 

I've come up with a solution using the BRepName stuff that you've alluded to.  For any element of a collection you can get the "BRepName" by

strBRepName = selection1.item2(1).Reference.DisplayName

although this gives you a bunch of extra characters and when trying to do this

set ref1 = part1.CreateReferenceFromBRep(strBRepName)

you get a runtime error.  So strBRepName needs to be trimmed.  In my case I'm pretty sure (a.k.a. this has worked so far) that the characters at the front of the string are "Selection_", so this gets rid of them

strBRepName = Replace(strBRepName,"Selection_",""

After trimming strBRepName I feed it to the CreateReferenceFromBRep method, which causes a runtime error, but I create a loop like so:

REDO_REFERENCE:

On Error Resume Next

Set reference1 = part1.CreateReferenceFromBRepName(strBRepName, solid1)

If Err.Number 0 Then

    strBRepName = Left(strBRepName, Len(strBRepName) - 1)
    Err.Clear
    GoTo REDO_REFERENCE

End If

I realize this may seem like too much work, but one thing that I like about it (I think) is that it avoids any problems with the versions of the BRepName (R15, R16, R17). 

Let me know if this is a stupid way of doing this

Steven Kwok

RE: Create Reference from Selection Object?
(in response to Paul Hamrick)
Personally, when it comes to VBA automation, there is no stupid solution, just no solution. Anyway, this worked in R14, you can try it out:

MsgBox "Select A Plane"
Set partDocument1 = CATIA.ActiveDocument
Set product1 = partDocument1.Product
Set MySelection = partDocument1.selection
InputObjectType(0) = "PlanarFace"
InputObjectType(1) = "Plane"
MySelection.Clear
Status = MySelection.SelectElement2(InputObjectType(), "Select a Plane", False)
If (i = 0 And Status "Normal" Then
    Errer = 3
    GoTo ErrerCodes
End If
Set SelectedPoints(0) = MySelection.Item(1).Value
Set Plane1Product = MySelection.FindObject("CATIAProduct"
Set Plane1Assembly = Plane1Product.Parent
Set reference3 = product1.CreateReferenceFromName(Plane1Assembly.Name & "/" & Plane1Product.Name & "/!" & SelectedPoints(0).Name)

'*YourSymmetryCodeHere*

Exit Sub
ErrerCodes:
If (Errer = 3) Then
    MsgBox "No plane selected, program termination."
End If


Oh, and by the way. You can declare all of the variables above if you like, EXCEPT for MySelection. You CANNOT declare MySelection as a selection or the code will fail. Feel free to declare it as a variant, tho (Dim MySelection)

Douglas Snell

RE: Create Reference from Selection Object?
(in response to Paul Hamrick)

Well I can't seem to figure out a great way of accomplishing your task.  I must have just be missing something in the first couple of posts.  Here is some workaround code you can tinker with if you want yet another way.

Sub catmain()

Dim doc As Document
Set doc = CATIA.ActiveDocument

If Right(doc.FullName, 7) "CATPart" Then
    MsgBox "Run in a part"
    Exit Sub
End If

Dim PartDoc As PartDocument
Set PartDoc = CATIA.ActiveDocument

Dim sel 'As Selection
Set sel = PartDoc.Selection

Dim oPart As Part
Set oPart = PartDoc.Part

Dim ShpFac As ShapeFactory
Set ShpFac = oPart.ShapeFactory

Dim hsf As HybridShapeFactory
Set hsf = oPart.HybridShapeFactory

Dim hbody As HybridBody
Set hbody = oPart.HybridBodies.Add

Dim status As String
Dim filter(0)

filter(0) = "PlanarFace"

status = sel.SelectElement2(filter, "Select Mirror Plane", False)

If status = "Cancel" Then
    MsgBox "Selection Canceled"
    Exit Sub
End If

Dim ref As Reference
Set ref = sel.Item(1).Value

Dim MirrorPlane As Plane
Set MirrorPlane = hsf.AddNewPlaneOffset(ref, 0, True)

MirrorPlane.Name = "MirrorPlane"

hbody.AppendHybridShape MirrorPlane

ShpFac.AddNewSymmetry2 MirrorPlane

End Sub