iLogic not placing DXF file in folder when running iLogic code.

iLogic not placing DXF file in folder when running iLogic code.

Thomas_Savage
Advisor Advisor
1,026 Views
4 Replies
Message 1 of 5

iLogic not placing DXF file in folder when running iLogic code.

Thomas_Savage
Advisor
Advisor

Hello,

I have a code that creates multiple DXF files for flat pattern sheet metal parts in my assembly. This code works fine on my computer at work. But will only work on that computer. It will not work on any other computers at work, or my home computer. The computer it works on is using Inventor 2016 and AutoCAD 2016 to open the DXF files. I have tried in Inventor 2016 at work and at home. And have just created new parts, assembly and drawings in Inventor 2017 and AutoCAD 2017 on my home computer. But it doesn't create or save the DXF files. The folder is just empty. But it says it has. Does anyone know why this could be? This is my code: Also as a Attachment.

 

Format:HTML Format Version:1.0 StartHTML: 165 EndHTML: 41356 StartFragment: 314 EndFragment: 41324 StartSelection: 314 EndSelection: 314SyntaxEditor Code Snippet

'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
oAsmName = Left(oAsmDoc.DisplayName, Len(oAsmDoc.DisplayName) -4)

'check that the active document is an assembly file
If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
Exit Sub
End If

'get user input
RUsure = MessageBox.Show ( _
"This will create a DXF file for all of the asembly components that have drawings files." _
& vbLf & "This rule expects that the drawing file shares the same name and location as the component." _
& vbLf & " " _
& vbLf & "Are you sure you want to create DXF Drawings for all of the assembly components?" _
& vbLf & "This could take a while.", "iLogic  - Batch Output DXFs ",MessageBoxButtons.YesNo)

If RUsure = vbNo Then
Return
Else
End If

oPath = ThisDoc.Path

'get DXF target folder path
oFolder = oPath & "\" & oAsmName & " DXF Files"

'Check for the DXF folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
    System.IO.Directory.CreateDirectory(oFolder)
End If



'[ DXF setup

' Get the DXF translator Add-In.
Dim DXFAddIn As TranslatorAddIn
DXFAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
'Set a reference to the active document (the document to be published).
Dim oDocument As Document
oDocument = ThisApplication.ActiveEditDocument
Dim oContext As TranslationContext
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
' Create a NameValueMap object
Dim oOptions As NameValueMap
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
' Create a DataMedium object
Dim oDataMedium As DataMedium
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

' Check whether the translator has 'SaveCopyAs' options
If DXFAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
    Dim strIniFile As String
    strIniFile = "C:\temp\dxfout.ini"
    ' Create the name-value that specifies the ini file to use.
    oOptions.Value("Export_Acad_IniFile") = strIniFile
End If

'] end of DXF setup


'[ ComponentDrawings 
    'look at the files referenced by the assembly
    Dim oRefDocs As DocumentsEnumerator
    oRefDocs = oAsmDoc.AllReferencedDocuments
    Dim oRefDoc As Document
    
    'work the the drawing files for the referenced models
    'this expects that the model has a drawing of the same path and name 
For Each oRefDoc In oRefDocs
    idwPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) - 3) & "idw"
    
    'check to see that the model has a drawing of the same path and name 
    If(System.IO.File.Exists(idwPathName)) Then
            Dim oDrawDoc As DrawingDocument
        oDrawDoc = ThisApplication.Documents.Open(idwPathName, True)
        oFileName = Left(oRefDoc.DisplayName, Len(oRefDoc.DisplayName) -3)
    
        On Error Resume Next ' if DXF exists and is open or read only, resume next
        'Set the DXF target file name
        oDataMedium.FileName = oFolder & "\" & oFileName & "DXF"
        'Write out the DXF
        'Set the destination file name
        oDataMedium.FileName = oFolder & "\" & oFileName & "dxf"
        'Publish document.
        Call DXFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
        'close the file
        oDrawDoc.Close
    Else
    'If the model has no drawing of the same path and name - do nothing
    End If
Next
'] End of ComponentDrawings 



'[ Top Level Drawing 
    oAsmDrawing = ThisDoc.ChangeExtension(".idw")
    oAsmDrawingDoc = ThisApplication.Documents.Open(oAsmDrawing, True)
    oAsmDrawingName = Left(oAsmDrawingDoc.DisplayName, Len(oAsmDrawingDoc.DisplayName) -3)
    'write out the DXF for the Top Level Assembly Drawing file
    On Error Resume Next ' if DXF exists and is open or read only, resume next
    'Set the DXF target file name
    oDataMedium.FileName = oFolder & "\" & oAsmDrawingName & "dxf"
    'Write out the DXF
    Call DXFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
    'Close the top level drawing
    oAsmDrawingDoc.Close
'] Top Level Drawing 

MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
'open the folder where the new ffiles are saved
Shell("explorer.exe " & oFolder,vbNormalFocus)

 



Thomas Savage

Design Engineer


0 Likes
Accepted solutions (3)
1,027 Views
4 Replies
Replies (4)
Message 2 of 5

Curtis_Waguespack
Consultant
Consultant

Hi Savage1989AZD9P ,

 

Comment out the "On Error Resume Next" lines and see what errors you get, if any.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com


@Savage1989AZD9P wrote:

 

'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
oAsmName = Left(oAsmDoc.DisplayName, Len(oAsmDoc.DisplayName) -4)

'check that the active document is an assembly file
If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
Exit Sub
End If

'get user input
RUsure = MessageBox.Show ( _
"This will create a DXF file for all of the asembly components that have drawings files." _
& vbLf & "This rule expects that the drawing file shares the same name and location as the component." _
& vbLf & " " _
& vbLf & "Are you sure you want to create DXF Drawings for all of the assembly components?" _
& vbLf & "This could take a while.", "iLogic  - Batch Output DXFs ",MessageBoxButtons.YesNo)

If RUsure = vbNo Then
Return
Else
End If

oPath = ThisDoc.Path

'get DXF target folder path
oFolder = oPath & "\" & oAsmName & " DXF Files"

'Check for the DXF folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
    System.IO.Directory.CreateDirectory(oFolder)
End If



'[ DXF setup

' Get the DXF translator Add-In.
Dim DXFAddIn As TranslatorAddIn
DXFAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
'Set a reference to the active document (the document to be published).
Dim oDocument As Document
oDocument = ThisApplication.ActiveEditDocument
Dim oContext As TranslationContext
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
' Create a NameValueMap object
Dim oOptions As NameValueMap
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
' Create a DataMedium object
Dim oDataMedium As DataMedium
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

' Check whether the translator has 'SaveCopyAs' options
If DXFAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
    Dim strIniFile As String
    strIniFile = "C:\temp\dxfout.ini"
    ' Create the name-value that specifies the ini file to use.
    oOptions.Value("Export_Acad_IniFile") = strIniFile
End If

'] end of DXF setup


'[ ComponentDrawings 
    'look at the files referenced by the assembly
    Dim oRefDocs As DocumentsEnumerator
    oRefDocs = oAsmDoc.AllReferencedDocuments
    Dim oRefDoc As Document
    
    'work the the drawing files for the referenced models
    'this expects that the model has a drawing of the same path and name 
For Each oRefDoc In oRefDocs
    idwPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) - 3) & "idw"
    
    'check to see that the model has a drawing of the same path and name 
    If(System.IO.File.Exists(idwPathName)) Then
            Dim oDrawDoc As DrawingDocument
        oDrawDoc = ThisApplication.Documents.Open(idwPathName, True)
        oFileName = Left(oRefDoc.DisplayName, Len(oRefDoc.DisplayName) -3)
    
        On Error Resume Next ' if DXF exists and is open or read only, resume next
        'Set the DXF target file name
        oDataMedium.FileName = oFolder & "\" & oFileName & "DXF"
        'Write out the DXF
        'Set the destination file name
        oDataMedium.FileName = oFolder & "\" & oFileName & "dxf"
        'Publish document.
        Call DXFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
        'close the file
        oDrawDoc.Close
    Else
    'If the model has no drawing of the same path and name - do nothing
    End If
Next
'] End of ComponentDrawings 



'[ Top Level Drawing 
    oAsmDrawing = ThisDoc.ChangeExtension(".idw")
    oAsmDrawingDoc = ThisApplication.Documents.Open(oAsmDrawing, True)
    oAsmDrawingName = Left(oAsmDrawingDoc.DisplayName, Len(oAsmDrawingDoc.DisplayName) -3)
    'write out the DXF for the Top Level Assembly Drawing file
    On Error Resume Next ' if DXF exists and is open or read only, resume next
    'Set the DXF target file name
    oDataMedium.FileName = oFolder & "\" & oAsmDrawingName & "dxf"
    'Write out the DXF
    Call DXFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
    'Close the top level drawing
    oAsmDrawingDoc.Close
'] Top Level Drawing 

MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
'open the folder where the new ffiles are saved
Shell("explorer.exe " & oFolder,vbNormalFocus)

 


 

EESignature

0 Likes
Message 3 of 5

Thomas_Savage
Advisor
Advisor
Accepted solution

It comes up with this error when I remove them lines of code:

 

 Error in rule : DXF, in document: AssemblyDXF Test.iam

 

The parameter is incorrect. ( Exception from HRESULT: 0x80070057(E_INVALIDARG



Thomas Savage

Design Engineer


0 Likes
Message 4 of 5

Thomas_Savage
Advisor
Advisor
Accepted solution

It comes up with the same error on my work computer. But works on my other work computer still. I just don't understand how it can do that. With my code, it runs the rule, reads the drawings, comes up with a message saying that the drawings are in a certain file, takes me to that file, but no files have been created.



Thomas Savage

Design Engineer


0 Likes
Message 5 of 5

Thomas_Savage
Advisor
Advisor
Accepted solution

I found the problem in the code. It was this part of the code:

 

oOptions.Value("Export_Acad_IniFile") = strIniFile

 

Which I changed to:

 

oOptions.Value(" ") = strIniFile

 

Now the code works fine.



Thomas Savage

Design Engineer