Constrain an assembly work axis to a hole in a part

Constrain an assembly work axis to a hole in a part

oransen
Collaborator Collaborator
1,591 Views
13 Replies
Message 1 of 14

Constrain an assembly work axis to a hole in a part

oransen
Collaborator
Collaborator

I understand that work axes at assembly level are either fixed (in space) or constrained.

 

oransen_0-1621611401152.png

 

 

I know how to constrain parts to each other in an assembly, but how do I programmatically create and constrain a work axis in an assembly to, for example a hole in a part? I can do it manually, so there must be a sequence of API calls to do it!

 

TIA

0 Likes
Accepted solutions (1)
1,592 Views
13 Replies
Replies (13)
Message 2 of 14

WCrihfield
Mentor
Mentor

Here is an example iLogic rule that will create a work axis within an assembly that coincides with the center axis of a hole in a part within.  My test assembly had two parts in it. Both parts had holes in them, but the target part only had one hole in it, that was made using a hole feature, instead of just an extruded cut feature.  So, if I want to specify that cylindrical face of that hole in the part without manually selecting it, I must navigate to that cylindrical face within the part.  Once I've got that face, I create a FaceProxy object from it, which brings its geometry over into the context of the assembly's 3D space, instead of that parts 3D space.  Then I extract the Cylinder geometry from that cylindrical face, and extract the needed Point and UnitVector objects/data needed from that.  Then I use the WorkAxes.AddFixed() method, supply those last two objects to it, and there you go.

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oComp As ComponentOccurrence = oADef.Occurrences.ItemByName("TestPart1:1")
Dim oPDoc As PartDocument = oComp.Definition.Document
Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition
Dim oHole As HoleFeature = oPDef.Features.HoleFeatures.Item(1)
'oHole.HoleCenterPoints 'could likely use these too
Dim oCylFace As Face = oHole.SideFaces.Item(1)
If oCylFace.SurfaceType = SurfaceTypeEnum.kCylinderSurface Then
	Dim oCylFaceProxy As FaceProxy
	oComp.CreateGeometryProxy(oCylFace, oCylFaceProxy)
	oCyl = oCylFaceProxy.Geometry
	Dim oPoint As Inventor.Point = oCyl.BasePoint
	Dim oAxis As UnitVector = oCyl.AxisVector
	oADef.WorkAxes.AddFixed(oPoint, oAxis, False)
Else
	MsgBox("Hole's first side face was not a Cylinder. Exiting.", , "")
	Exit Sub
End If

 

You could likely condense this code a bit more if you wanted, but the process of how to do what you wanted should be fairly easy for you to follow.  The key in this process is creating the FaceProxy, as you may have guessed.

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 14

oransen
Collaborator
Collaborator

@WCrihfield, thanks. Quick question, you use AddFixed. If you move the part, do the axes move to?

 

 

0 Likes
Message 4 of 14

oransen
Collaborator
Collaborator

Hello all, anybody know how to do this? I was maybe not very explicit in the original post, but I need to constrain rather than fix the axis, which is why the arrows are pointing from Mate:1.

 

My C# version of @WCrihfield 's post shows that AddFixed does not constrain, it places an axis in a fixed position in the assembly's space. Moving the part does not move the axis.

 

It can be done by hand, so surely it can be done using the API....?

 

0 Likes
Message 5 of 14

WCrihfield
Mentor
Mentor

I guess I wasn't sure about that part of the request.  OK, that's doable too.

I have updated that code to constrain the axis to the parts hole, after it is created.

I just added a variable name in front of the line that creates the work axis, then I use that variable within the line to constrain it.  Then the line to constrain it (it's a long line of code, so you could wrap it to another line if needed).

Try this:

 

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oComp As ComponentOccurrence = oADef.Occurrences.ItemByName("TestPart1:1")
Dim oPDoc As PartDocument = oComp.Definition.Document
Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition
Dim oHole As HoleFeature = oPDef.Features.HoleFeatures.Item(1)
'oHole.HoleCenterPoints 'could likely use these too
Dim oCylFace As Face = oHole.SideFaces.Item(1)
If oCylFace.SurfaceType = SurfaceTypeEnum.kCylinderSurface Then
	Dim oCylFaceProxy As FaceProxy
	oComp.CreateGeometryProxy(oCylFace, oCylFaceProxy)
	oCyl = oCylFaceProxy.Geometry
	Dim oPoint As Inventor.Point = oCyl.BasePoint
	Dim oAxis As UnitVector = oCyl.AxisVector
	oWAxis = oADef.WorkAxes.AddFixed(oPoint, oAxis, False)
	Dim oMConst1 As MateConstraint = oADef.Constraints.AddMateConstraint2(oWAxis, oCylFaceProxy,0,InferredTypeEnum.kInferredLine,InferredTypeEnum.kInferredLine, MateConstraintSolutionTypeEnum.kAlignedSolutionType)
Else
	MsgBox("Hole's first side face was not a Cylinder. Exiting.", , "")
	Exit Sub
End If

 

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 14

oransen
Collaborator
Collaborator

Thanks for that, Let me see if I can reproduce this in C#....

0 Likes
Message 7 of 14

oransen
Collaborator
Collaborator

I'm not too familiar with VB, but I tried to get you code going, but have the problems on the two lines marked  ' HERE

 

Imports System.Runtime.InteropServices
Imports Inventor


Class MainWindow
    Private Sub Button_Click(sender As Object, e As RoutedEventArgs)

        Dim ThisApp As Inventor.Application
        Dim oCyl As FaceProxy
        Dim oWAxis As WorkAxis

        ThisApp = Marshal.GetActiveObject("Inventor.application")


        If ThisApp.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
            MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbOKOnly + vbCritical, "WRONG DOCUMENT TYPE")
            Exit Sub
        End If


        Dim oADoc As AssemblyDocument = ThisApp.ActiveDocument
        Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
        Dim oComp As ComponentOccurrence = oADef.Occurrences.Item(1) ' ByName("Bent-Part-With-Hole:1")
        Dim oPDoc As PartDocument = oComp.Definition.Document
        Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition
        If oPDef.Features.HoleFeatures.Count = 0 Then
            MsgBox("No holes")
            Exit Sub
        End If

        Dim oHole As HoleFeature = oPDef.Features.HoleFeatures.Item(1)

        'oHole.HoleCenterPoints 'could likely use these too
        Dim oCylFace As Face = oHole.SideFaces.Item(1)
        If oCylFace.SurfaceType = SurfaceTypeEnum.kCylinderSurface Then
            Dim oCylFaceProxy As FaceProxy
            oComp.CreateGeometryProxy(oCylFace, oCylFaceProxy) ' HERE I get a warning
            oCyl = oCylFaceProxy.Geometry                      ' HERE i get a crash   
            Dim oPoint As Inventor.Point = oCyl.BasePoint
            Dim oAxis As UnitVector = oCyl.AxisVector
            oWAxis = oADef.WorkAxes.AddFixed(oPoint, oAxis, False)
            Dim oMConst1 As MateConstraint = oADef.Constraints.AddMateConstraint2(oWAxis, oCylFaceProxy, 0, InferredTypeEnum.kInferredLine, InferredTypeEnum.kInferredLine, MateConstraintSolutionTypeEnum.kAlignedSolutionType)

        Else
            MsgBox("Hole's first side face was not a Cylinder. Exiting.", , "")
            Exit Sub
        End If

    End Sub
End Class

 

What have I done wrong do you think?

 

0 Likes
Message 8 of 14

FINET_Laurent
Advisor
Advisor

Hi,

 

I don't think you need the line (you can remove it) :

oCyl = oCylFaceProxy.Geometry                      ' HERE i get a crash   

Here is an example of mine creating a constraint with proxy :

Dim oDoc As Document = ThisApplication.ActiveDocument 
Dim oAssCompDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
Dim oCst As AssemblyConstraints = oAssCompDef.Constraints
Dim oOcc As ComponentOccurrence

oOcc = oAssCompDef.Occurrences.ItemByName("Upstream header:1")

Dim oOccWorkPlane As WorkPlane = oOcc.Definition.WorkPlanes.Item("Header axis")

Dim oWorkPlane As WorkPlane = oAssCompDef.WorkPlanes.Item("Plan YZ")
Dim oproxyPlane1 As WorkPlaneProxy
oOcc.CreateGeometryProxy(oOccWorkPlane, oproxyPlane1)

oCst.AddMateConstraint(oproxyPlane1,oWorkPlane,10)

 Hope this helps..

 

Regards,

 

FINET L.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

0 Likes
Message 9 of 14

oransen
Collaborator
Collaborator

@FINET_Laurent, my understanding of proxies was that they give access to the coordinates of features in parts, but in the assembly coordinate system.

 

If I get rid of oCyl I cannot gets its geometry to call AddFixed. Though I'm suspicious of AddFixed...

 

If I don't use oCylFaceProxy in my call to AddMateConstraint2, what do I use?

 

 

0 Likes
Message 10 of 14

FINET_Laurent
Advisor
Advisor
oCyl = oCylFaceProxy.Geometry 

oCyl is declared as FaceProxy and not as a FaceProxy.Geometry object.

 

Personnaly when doing this I do  it like this (create an axis on 0-0-0 & constraint it to the hole) :

 

Dim oPoint As Point = ThisApplication.TransientGeometry.CreatePoint(0, 0, 0)

Dim oAxis As WorkAxis = oAsscompDef.WorkAxes.AddFixed(oPoint, ThisApplication.TransientGeometry.CreateUnitVector(0, 0, 1))

Dim oMate1 As MateConstraint = oConstraints.AddMateConstraint2(oAxis, oFace1, 0, kNoInference, kInferredLine, kUndirectedSolutionType)

oFace is the circular face.

I tried giving the vector random directions and it still does the trick. Maybe have a try.

I'm at the office so I can't look closely.. :C

 

Regards,

 

FINET L.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

Message 11 of 14

oransen
Collaborator
Collaborator
I'll give it a go...thanks!
Message 12 of 14

WCrihfield
Mentor
Mentor
Accepted solution

Apparently I didn't declare the Type of the oCyl variable in my original code, but it was intended to be a 'Cylinder' object (Transient mathematical geometry), not a FaceProxy or Face.  It is the underlying geometry object that makes up that specific FaceProxy object (because the face was cylindrical shaped in my example).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 13 of 14

WCrihfield
Mentor
Mentor

@oransen 

Also if oADef.Occurrences.Item(1) doesn't work for you, and you would rather supply the component's name (as you hinted at in your later posted code comment), then you could use this instead:

 

Dim oComp As ComponentOccurrence = oADef.Occurrences.ItemByName("Bent-Part-With-Hole:1")

 

I just used Item(1) in my example, because it was simple and generic, and just happened to be the right component in my test assembly.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 14 of 14

oransen
Collaborator
Collaborator

@WCrihfield, aha, that's done it! Now the axis moves with the part which is what I needed. Here is the full working code for future searchers after truth. Thanks again!

 

    Private Sub Button_Click(sender As Object, e As RoutedEventArgs)

        Dim ThisApp As Inventor.Application
        Dim oCylGeom As Cylinder
        Dim oWAxis As WorkAxis

        ThisApp = Marshal.GetActiveObject("Inventor.application")


        If ThisApp.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
            MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbOKOnly + vbCritical, "WRONG DOCUMENT TYPE")
            Exit Sub
        End If


        Dim oADoc As AssemblyDocument = ThisApp.ActiveDocument
        Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
        Dim oComp As ComponentOccurrence = oADef.Occurrences.Item(1) ' ByName("Bent-Part-With-Hole:1")
        Dim oPDoc As PartDocument = oComp.Definition.Document
        Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition
        If oPDef.Features.HoleFeatures.Count = 0 Then
            MsgBox("No holes")
            Exit Sub
        End If

        Dim oHole As HoleFeature = oPDef.Features.HoleFeatures.Item(1)

        'oHole.HoleCenterPoints 'could likely use these too
        Dim oCylFace As Face = oHole.SideFaces.Item(1)
        If oCylFace.SurfaceType = SurfaceTypeEnum.kCylinderSurface Then
            Dim oCylFaceProxy As FaceProxy = Nothing
            oComp.CreateGeometryProxy(oCylFace, oCylFaceProxy)
            oCylGeom = oCylFaceProxy.Geometry
            Dim oPoint As Inventor.Point = oCylGeom.BasePoint
            Dim oAxis As UnitVector = oCylGeom.AxisVector
            oWAxis = oADef.WorkAxes.AddFixed(oPoint, oAxis, False)
            Dim oMConst1 As MateConstraint = oADef.Constraints.AddMateConstraint2(oWAxis, oCylFaceProxy, 0, InferredTypeEnum.kInferredLine, InferredTypeEnum.kInferredLine, MateConstraintSolutionTypeEnum.kAlignedSolutionType)

        Else
            MsgBox("Hole's first side face was not a Cylinder. Exiting.", , "")
            Exit Sub
        End If

    End Sub

 

 

0 Likes