VBA - Assembly Constraint between hole of part to workplane of another part.

VBA - Assembly Constraint between hole of part to workplane of another part.

sam
Advocate Advocate
850 Views
4 Replies
Message 1 of 5

VBA - Assembly Constraint between hole of part to workplane of another part.

sam
Advocate
Advocate

Hi,

 

I have been able to get to hole feature of a part and workplane of another part inside assembly but now I am struggling to write the syntax of constraint. Any help will be appreciated. 

 

Regards, 

Sam

0 Likes
Accepted solutions (1)
851 Views
4 Replies
Replies (4)
Message 2 of 5

JhoelForshav
Mentor
Mentor

@sam 

In order to make a constraint like this you'll have to create geometry proxies for the geometries you want to constrain to each other. It's the proxy objects that should be used as arguments when creating the constraint. Could you share some non confidential files and also some more information? for example what type of constraint you want to make? 🙂

Message 3 of 5

sam
Advocate
Advocate

Hi, 

@JhoelForshav Thanks for the reply. I am sorry I Can't access the files but i can access code after changing some of the names. So first two constraints as shown below are working also I can manually add third mate constrain (between plane and hole feature axis) but trying and failing to add it through VBA. Thanks in advance. 

Set oWorkPlane01 = oOccu.Definition.WorkPlanes.Item(3)
            Set oWorkPlane02 = Occu01.Definition.WorkPlanes.Item(2)
            Call oOccu.CreateGeometryProxy(oWorkPlane01, oAsmPlane01)
            Call Occu01.CreateGeometryProxy(oWorkPlane02, oAsmPlane02)
            Call oAsmCompDef.Constraints.AddFlushConstraint(oAsmPlane01, oAsmPlane02, 0)
            '002 Flush Constraint 01
            Set oWorkPlane01 = oOccu.Definition.WorkPlanes.Item(2)
            Set oWorkPlane02 = Occu01.Definition.WorkPlanes.Item(3)
            Call oOccu.CreateGeometryProxy(oWorkPlane01, oAsmPlane01)
            Call Occu01.CreateGeometryProxy(oWorkPlane02, oAsmPlane02)
            Call oAsmCompDef.Constraints.AddFlushConstraint(oAsmPlane01, oAsmPlane02, 0)
            '003 Mate Constraint 02
            Dim oHole As HoleFeature
            'Dim oWorkAxix As WorkAxisProxy
            Set PartComponentDefinition = oOccu.Definition
            Set oHole = oOccu.Definition.Features.HoleFeatures.Item("holeName")
            'MsgBox (oHole.Name)
            'I have checked I can access the hole feature, obviously need to add more code to access axis (may be?)
            Set oWorkPlane02 = Occu01.Definition.WorkPlanes.Item(1)
            Call Occu01.CreateGeometryProxy(oWorkPlane02, oAsmPlane02)
            Call oAsmCompDef.Constraints.AddMateConstraint(oHole, oAsmPlane02, 0)

 

0 Likes
Message 4 of 5

JhoelForshav
Mentor
Mentor
Accepted solution

Ok @sam ,

I assume you want to use the center axis of the hole? A constraint is not actually using the holefeature, and even if it were you'd have to create a proxy for that aswell... If you want to create a mate constraint of the center axis of a hole and a face, you'd have to get a proxy of the face just as you did. Then you must get a proxy for the face created by the hole (The cylindrical face) and use that. You cannot just use it without specifying the inferredtype though. In this case we want to use the center axis of the face, so the inferredtype should be kInferredLine.

 

I put together a quick ilogic rule for you to demonstrate this. Make an assembly containing part occurrences and name them A and B in the browser. Make sure occurrence B has a hole in it. Then run this rule in your assembly.

 

Dim oAsm As AssemblyDocument = ThisDoc.Document
Dim oAsmDef As AssemblyComponentDefinition = oAsm.ComponentDefinition

Dim oOcc1 As ComponentOccurrence = oAsmDef.Occurrences.ItemByName("A")
Dim oOcc2 As ComponentOccurrence = oAsmDef.Occurrences.ItemByName("B")
Dim oPlaneProx As WorkPlaneProxy
oOcc1.CreateGeometryProxy(oOcc1.Definition.WorkPlanes(1), oPlaneProx)
Dim oHoleFaceProx As FaceProxy
oOcc2.CreateGeometryProxy(oOcc2.Definition.Features.HoleFeatures(1).Faces(1), oHoleFaceProx)
oAsm.SelectSet.Select(oHoleFaceProx)

oAsmDef.Constraints.AddMateConstraint(oPlaneProx, oHoleFaceProx, 0, InferredTypeEnum.kNoInference, InferredTypeEnum.kInferredLine)

I hope this helps 🙂

Message 5 of 5

sam
Advocate
Advocate

Hi @JhoelForshav , 

 

Thanks for the help. It is working perfectly. made some changes to suit. 

 

best regards, 

sam

'001 Mate Constraint 01
            Set oWorkPlane01 = oOccu.Definition.WorkPlanes.Item(3)
            Set oWorkPlane02 = Occu01.Definition.WorkPlanes.Item(2)
            Call oOccu.CreateGeometryProxy(oWorkPlane01, oAsmPlane01)
            Call Occu01.CreateGeometryProxy(oWorkPlane02, oAsmPlane02)
            Call oAsmCompDef.Constraints.AddFlushConstraint(oAsmPlane01, oAsmPlane02, 0)
            '002 Flush Constraint 01
            Set oWorkPlane01 = oOccu.Definition.WorkPlanes.Item(2)
            Set oWorkPlane02 = Occu01.Definition.WorkPlanes.Item(3)
            Call oOccu.CreateGeometryProxy(oWorkPlane01, oAsmPlane01)
            Call Occu01.CreateGeometryProxy(oWorkPlane02, oAsmPlane02)
            Call oAsmCompDef.Constraints.AddFlushConstraint(oAsmPlane01, oAsmPlane02, 0)
            '003 Mate Constraint 02
            Dim oHole As HoleFeature
            Dim oWorkAxix As WorkAxisProxy
            Set PartComponentDefinition = oOccu.Definition
            Dim oHoleFaceProx1 As FaceProxy
            Call oOccu.CreateGeometryProxy(oOccu.Definition.Features.HoleFeatures.Item("HoleName").Faces(1), oHoleFaceProx1)
            Set oWorkPlane02 = Occu01.Definition.WorkPlanes.Item(1)
            Call Occu01.CreateGeometryProxy(oWorkPlane02, oAsmPlane02)
            Call oAsmCompDef.Constraints.AddMateConstraint(oAsmPlane02, oHoleFaceProx1, 0, InferredTypeEnum.kNoInference, InferredTypeEnum.kInferredLine)