Unwrap export fails when part is in an assembly

Unwrap export fails when part is in an assembly

treverFWB8S
Explorer Explorer
307 Views
3 Replies
Message 1 of 4

Unwrap export fails when part is in an assembly

treverFWB8S
Explorer
Explorer

I have a subroutine that takes a face, unwraps it, and exports it as a DXF. This subroutine works as expected when the PartDocument is on its own but does not export a DXF when the PartDocument is a child of an assembly, it also does not produce an error upon failing. I've confirmed that the unwrap is created successfully, and the rest of my code works fine with a PartDocument that is part of an AssemblyDocument.

Any help is appreciated, let me know if I need to provide more context.

Here's the subroutine:

Private Sub Unwrap(oFace As Face)
    Dim oFaceCollection As FaceCollection = oInvApp.TransientObjects.CreateFaceCollection()
    oFaceCollection.Add(oFace)
    Dim oUnwrapDef As UnwrapDefinition = oPartDoc.ComponentDefinition.Features.UnwrapFeatures.CreateDefinition(oFaceCollection)
    Dim oUnwrapFeature As UnwrapFeature = oPartDoc.ComponentDefinition.Features.UnwrapFeatures.Add(oUnwrapDef)

    Dim oUnwrappedFace As Face = oUnwrapFeature.SurfaceBody.Faces(1)
    Dim oSelect As SelectSet = oPartDoc.SelectSet
    Dim sFileType = oPartDoc.DisplayName.Split(".").Last()
    Dim sUnwrapFilename As String = Left(oPartDoc.DisplayName, (oPartDoc.DisplayName.Length - (sFileType.Length + 1))) & ".dxf"

    oSelect.Select(oUnwrappedFace)

    If oSelect.Count > 0 Then
        Console.WriteLine("saving dxf: " & sDestinationFolder & "\" & sCustomerName & "\" & sUnwrapFilename)
         oInvApp.CommandManager.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, (sDestinationFolder & "\" & sCustomerName & "\" & sUnwrapFilename))
         oInvApp.CommandManager.ControlDefinitions.Item("GeomToDXFCommand").Execute2(True)
    End If

    oSelect.Clear()
End Sub

 

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

WCrihfield
Mentor
Mentor
Accepted solution

I think I see why your code seems to work OK when ran directly on a part, when that part is the 'active' document, but not when the part is within an assembly, and that assembly is the 'active' document.  it is because you are relying on the process of [selection and command execution], which is attempting to 'simulate user interactions', instead of using true Inventor API or iLogic tools.  Selection is only possible within the 'active' document, so you can not select things that are defined within other referenced documents.  Also, most commands (ControlDefinitions) will only attempt to act upon the currently 'active' document, and/or whatever is currently selected within the active document.  Generally speaking, when an assembly is 'active' when your code starts, it will remain the active document while that code is running, even if iterating through other referenced documents.  So, if you need to export the profiles of those faces to DXF files, and do it from the context of an assembly, then you will likely need to use a different export process...one based in Inventor API code, instead of selections and command executions.

One process I use sometimes is, create a temporary sketch sketch on the planar face, then project all of the face's edges, make sure they are not construction geometry, then export that sketch to DXF using its PlanarSketch.DataIO.WriteDataToFile(sFormat, sFileName) method.  It does not give us the opportunity to specify any 'options' for the export, other than file type, and file name, but it works OK for me most of the time.  I usually use an Inventor.Transaction while doing this, so that I can UNDO that transaction to get rid of the sketch afterwards, as if nothing happened, which leaves the DXF file alone.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 4

treverFWB8S
Explorer
Explorer

Thanks for your response! Your solution didn't quite work for me (moving the unwrap to a sketch would have had the same issues) but it did point me in the right direction. I ended up saving off the part document as an IPT, opening that file directly, and then deleting it when I was done.

0 Likes
Message 4 of 4

treverFWB8S
Explorer
Explorer

Turns out I was wrong and Wesley was correct. My solution of opening a file as a separate ipt caused the same issue (I'm not sure why I thought it was solved) but ultimately I corrected it, here's a code snippet for prosperities sake: 

Dim oFaceCollection As FaceCollection = oInvApp.TransientObjects.CreateFaceCollection()
oFaceCollection.Add(oFace)

Dim oUnwrapDef As UnwrapDefinition = oPartDoc.ComponentDefinition.Features.UnwrapFeatures.CreateDefinition(oFaceCollection)

Dim oUnwrapFeature As UnwrapFeature = oPartDoc.ComponentDefinition.Features.UnwrapFeatures.Add(oUnwrapDef)

Dim oUnwrappedFace As Face = oUnwrapFeature.SurfaceBody.Faces(1)

Dim oPlanarSketch As PlanarSketch = oPartDoc.ComponentDefinition.Sketches.Add(oUnwrappedFace, True)

Dim sOut As String = "DXF"
oPlanarSketch.DataIO.WriteDataToFile(sOut, sFileName)
0 Likes