VBA Named face joint error

VBA Named face joint error

peter.ensing
Enthusiast Enthusiast
388 Views
2 Replies
Message 1 of 3

VBA Named face joint error

peter.ensing
Enthusiast
Enthusiast

Hello all!

 

So I'm trying to ridig joint an assembly to another item. This other item is simply picked with the commandmanager.
Inside the assembly template is a part with a named face. I want the user only need to pick the hole and it'll create the joint. 
But I get an runtime error. Somebody knows whats going wrong? (the code works if I replace the named face code for just another commandmanager face pick)

Public Sub AssemblyConnect()
    ' Create a new assembly document.
    Dim asmDoc As AssemblyDocument
    Set asmDoc = ThisApplication.ActiveDocument
    Dim asmDef As AssemblyComponentDefinition
    Set asmDef = asmDoc.ComponentDefinition

  Dim oOcc As ComponentOccurrence
  Set oOcc = asmDoc.SelectSet(1)
  
Dim occsub As ComponentOccurrence
Set occsub = oOcc.SubOccurrences.Item(2)

Dim oPartDoc As PartDocument
Set oPartDoc = occsub.Definition.Document

Dim oFace2 As face
Set oFace2 = oPartDoc.AttributeManager.FindObjects(, , "Jointface")(1)

    Dim oAdoc As AssemblyDocument
    Set oAdoc = oOcc.Definition.Document
    
    Dim oFace1 As Edge

    Set oFace1 = ThisApplication.CommandManager.Pick(kPartEdgeCircularFilter, "select a hole")
    
    ' Create two intents to define the geometry for the connection.
    Dim intentOne As GeometryIntent
    Set intentOne = asmDef.CreateGeometryIntent(oFace1, PointIntentEnum.kCenterPointIntent)
    Dim intentTwo As GeometryIntent
    Set intentTwo = asmDef.CreateGeometryIntent(oFace2, PointIntentEnum.kPlanarFaceCenterPointIntent)
    
    ' Create a rigid connection between the two parts.
    Dim connectDef As AssemblyJointDefinition
    Set connectDef = asmDef.Joints.CreateAssemblyJointDefinition(kRigidConnectionType, intentTwo, intentOne)
    connectDef.FlipAlignmentDirection = False
    connectDef.FlipOriginDirection = False
    Dim connect As AssemblyJoint
    Set connect = asmDef.Joints.Add(connectDef)
    
    ' Make the connection visible.
    connect.Visible = False
End Sub

Thanks in advance and kind regards,

Peter

 

0 Likes
Accepted solutions (1)
389 Views
2 Replies
Replies (2)
Message 2 of 3

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @peter.ensing 

The reason it works with pick method is because it creates geometry proxy for the picked geometry that the top level assembly can use. An assembly doesn't contain the actual geometry that is in the parts. Its basically just a structure of references. To work with the geometry in the assembly you'll have to reference it through a proxy. I copied your code and added a few lines to make a proxy for the named face:

 

Public Sub AssemblyConnect()
' Create a new assembly document.
Dim asmDoc As AssemblyDocument
Set asmDoc = ThisApplication.ActiveDocument
Dim asmDef As AssemblyComponentDefinition
Set asmDef = asmDoc.ComponentDefinition

Dim oOcc As ComponentOccurrence
Set oOcc = asmDoc.SelectSet(1)

Dim occsub As ComponentOccurrence
Set occsub = oOcc.SubOccurrences.Item(2)

Dim oPartDoc As PartDocument
Set oPartDoc = occsub.Definition.Document

Dim oFace2 As Face
Set oFace2 = oPartDoc.AttributeManager.FindObjects(, , "Jointface")(1)

'Create the face proxy
Dim oFace2Prox As FaceProxy
Call occsub.CreateGeometryProxy(oFace2, oFace2Prox)
Call oOcc.CreateGeometryProxy(oFace2Prox, oFace2Prox)


Dim oFace1 As Edge

Set oFace1 = ThisApplication.CommandManager.Pick(kPartEdgeCircularFilter, "select a hole")

' Create two intents to define the geometry for the connection.
Dim intentOne As GeometryIntent
Set intentOne = asmDef.CreateGeometryIntent(oFace1, PointIntentEnum.kCenterPointIntent)
Dim intentTwo As GeometryIntent
'Use the proxy objext instead
Set intentTwo = asmDef.CreateGeometryIntent(oFace2Prox, PointIntentEnum.kPlanarFaceCenterPointIntent)

' Create a rigid connection between the two parts.
Dim connectDef As AssemblyJointDefinition
Set connectDef = asmDef.Joints.CreateAssemblyJointDefinition(kRigidConnectionType, intentTwo, intentOne)
connectDef.FlipAlignmentDirection = False
connectDef.FlipOriginDirection = False
Dim connect As AssemblyJoint
Set connect = asmDef.Joints.Add(connectDef)

' Make the connection visible.
connect.Visible = False
End Sub

 

 

Message 3 of 3

peter.ensing
Enthusiast
Enthusiast

@JhoelForshavThis works like a charm, thanks for that!

Also thanks for the explanation why it didnt work, makes sense now...