Flipping direction of a Hem feature

By Balaji Ramamoorthy

The Hem feature relies on an edge to decide its direction. When editing the hem feature to flip its direction, Inventor identifies another edge and uses it for the hem feature. To do this programmatically, the HemDefinition.Edges collection is to be set. Currently the HemDefinition.Edges displays a “NotImplemented” error when trying to set it.

A workaround is to create a new Hem feature using the HemDefinition from the existing feature and use another edge in the process. The existing feature can be deleted after the new one is created. Also, it is important to set the end of part for retrieving the Edges collection from a Hem feature.

Here is a sample iLogic and C# code to flip the direction:

iLogic code snippet:

Dim oPartDoc As PartDocument
oPartDoc = ThisApplication.ActiveDocument

Dim oSheetMetalCompDef As SheetMetalComponentDefinition
oSheetMetalCompDef = oPartDoc.ComponentDefinition

Dim smFeatures As SheetMetalFeatures
smFeatures = oSheetMetalCompDef.Features

Dim oHems As HemFeatures
oHems = smFeatures.HemFeatures

Dim oHemFeature As HemFeature
oHemFeature = oHems.Item(1)

Dim oHemDef As HemDefinition
oHemDef = oHemFeature.Definition

' Important to get the right edge
oHemFeature.SetEndOfPart(True)

' current hem edge vertices
Dim currentHemEdge As Edge

Dim oHemEdgeColl As EdgeCollection
oHemEdgeColl = oHemDef.Edges
currentHemEdge = oHemDef.Edges.Item(1)

Dim oStartVertex As Vertex
oStartVertex = currentHemEdge.StartVertex

Dim oStopVertex As Vertex
oStopVertex = currentHemEdge.StopVertex

Dim edge1 As Edge
edge1 = oSheetMetalCompDef.SurfaceBodies(1).Edges(1)

Dim edge2 As Edge
edge2 = oSheetMetalCompDef.SurfaceBodies(1).Edges(3)

Dim oEdgeColl As EdgeCollection
oEdgeColl = ThisApplication.TransientObjects.CreateEdgeCollection

If edge1.StartVertex Is oStartVertex And edge1.StopVertex Is oStopVertex Then
' edge1 is the hem edge.
' Let's use another edge to flip the hem feature
    oEdgeColl.Add(edge2)
Else If edge2.StartVertex Is oStartVertex And edge2.StopVertex Is oStopVertex Then
' edge2 is the hem edge.
' Let's use another edge to flip the hem feature
    oEdgeColl.Add(edge1)
Else
    MessageBox.Show("Sorry, could not flip direction." & vbCrLf & _
    "The hem Feature does Not use a known edge", _
    "iLogic HemFeature Flip direction")

    oHemFeature.SetEndOfPart(False)
    Return
End If

Dim oHemDefNew As HemDefinition
oHemDefNew = oHemDef.Copy
oHemDefNew.Edges = oEdgeColl

Dim hemFeatureName As String
hemFeatureName = oHemFeature.Name

' Delete the existing hem feature
oHemFeature.Delete

Dim oHemFeatureNew As HemFeature
oHemFeatureNew = oHems.Add(oHemDefNew)
oHemFeatureNew.Name = hemFeatureName

C# code snippet:

PartDocument oPartDoc
    = (PartDocument)_invApp.ActiveDocument;

SheetMetalComponentDefinition oSheetMetalCompDef
    = oPartDoc.ComponentDefinition
    as SheetMetalComponentDefinition;

SheetMetalFeatures smFeatures
    = oSheetMetalCompDef.Features as SheetMetalFeatures;

HemFeatures oHems = smFeatures.HemFeatures;

HemFeature oHemFeature = oHems[1];
HemDefinition oHemDef = oHemFeature.Definition;

// Important to get the right edge from the HemDefinition
oHemFeature.SetEndOfPart(true);

EdgeCollection oHemEdgeColl = oHemDef.Edges;
Edge currentHemEdge = oHemDef.Edges[1];

Vertex oStartVertex = currentHemEdge.StartVertex;

Vertex oStopVertex = currentHemEdge.StopVertex;

Edge edge1
    = oSheetMetalCompDef.SurfaceBodies[1].Edges[1];

Edge edge2
    = oSheetMetalCompDef.SurfaceBodies[1].Edges[3];

EdgeCollection oEdgeColl =
    _invApp.TransientObjects.CreateEdgeCollection();

if (object.ReferenceEquals(edge1.StartVertex, oStartVertex) &&
    object.ReferenceEquals(edge1.StopVertex, oStopVertex))
{
    // edge1 is the hem edge.
    // Let's use another edge to flip the hem feature
    oEdgeColl.Add(edge2);
}
else if (object.ReferenceEquals(edge2.StartVertex, oStartVertex) &&
    object.ReferenceEquals(edge2.StopVertex, oStopVertex))
{
    // edge2 is the hem edge.
    // Let's use another edge to flip the hem feature
    oEdgeColl.Add(edge1);
}
else
{
    MessageBox.Show(@"Sorry, could not flip direction.
        The hem feature does not use a known edge",
        "HemFeature Flip direction");
    oHemFeature.SetEndOfPart(false);
    return;
}

HemDefinition oHemDefNew = oHemDef.Copy();
oHemDefNew.Edges = oEdgeColl;

string hemFeatureName = null;
hemFeatureName = oHemFeature.Name;

// Delete the existing hem feature
oHemFeature.Delete();

HemFeature oHemFeatureNew = default(HemFeature);
oHemFeatureNew = oHems.Add(oHemDefNew);
oHemFeatureNew.Name = hemFeatureName;

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading