Error in external iLogic rule - export DXF from IDW then close

Error in external iLogic rule - export DXF from IDW then close

andrew.tom.reynolds
Advocate Advocate
1,326 Views
12 Replies
Message 1 of 13

Error in external iLogic rule - export DXF from IDW then close

andrew.tom.reynolds
Advocate
Advocate

I've got an external ilogic rule which exports a DXF from the open IDW file and i'm then wanting the file to close.

 

The DXF export portion of the file runs fine, but as soon as I add ThisDoc.Document.Close(True) to the last line, I get an error dialog with "Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))".

 

I have tried the ThisDoc.Document.Close(True) in it's own rule and it works without error. I have also tried adding it as the last line in my export PDF from IDW external rule and it also works without error for PDFs. The problem is in this DXF export rule.

 

Can anyone offer some advice? I'm guessing it's probably still executing the DXF export and tries to close the drawing in the middle of the process or something?

 

'Default top level directory
oPath = "U:\INVENTOR PARTS\"

' 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.ActiveDocument

Dim oContext As TranslationContext
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = 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:\Inventor 2022\Tweed DXF Output Settings.ini"

' Create the name-value that specifies the ini file to use.
oOptions.Value("Export_Acad_IniFile") = strIniFile
End If

'Get the iProperties from the model and create the file name string
Dim oMdoc As Document
oMdoc = ThisDrawing.ModelDocument

Dim oProp1Value As String
oProp1Value = oMdoc.PropertySets("Design Tracking Properties").Item("Cost Center").Value
Dim oProp2Value As String
oProp2Value = oMdoc.PropertySets("Design Tracking Properties").Item("Project").Value
Dim oProp3Value As String
oProp3Value = oMdoc.PropertySets("Design Tracking Properties").Item("Part Number").Value

'Check if subfolder exists and/or create it
oFolder = oPath & oProp1Value & "\"

If Not System.IO.Directory.Exists(oFolder) Then
	System.IO.Directory.CreateDirectory(oFolder)
End If

oDataMedium.FileName = oFolder & oProp1Value & " - " & oProp2Value & " - " & oProp3Value & ".dxf"

'Publish document.
Call DXFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)

ThisDoc.Document.Close(True)
0 Likes
Accepted solutions (1)
1,327 Views
12 Replies
Replies (12)
Message 2 of 13

WCrihfield
Mentor
Mentor

It's in how you identify which document you're working with.  Pick one document reference that works, and use that same document variable throughout your code to avoid misrepresentation and the potential error that come with them.  ThisApplication.ActiveDocument can be referring to a different document than ThisDoc.Document.  I have a contribution post about this very subject you might find helpful.

 

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 13

andrew.tom.reynolds
Advocate
Advocate

Hi @WCrihfield , I understand what you're saying but i've tried a few variations and still the same result. What specifically in this case would you change in the code and i'll give it a try?

0 Likes
Message 4 of 13

WCrihfield
Mentor
Mentor

Since you have already created a variable near the top of your code:

Dim oDocument As Document
oDocument = ThisApplication.ActiveDocument

 for the active document, and it seems to be working, I would use that same document variable again at the end to close it like this:

oDocument.Close(True) 'True means don't save it when closing

 Here's the online help link to the page about that Close method.

 

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 5 of 13

andrew.tom.reynolds
Advocate
Advocate

This is the exact line of code I had in there when I first created the script and it doesn't work either sadly. Get the same error message. I just tried again to be sure... "Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))"

0 Likes
Message 6 of 13

A.Acheson
Mentor
Mentor

@andrew.tom.reynolds 

 

I tested that yesterday in Inv2020 and it works using

oDocument.Close(True) 

 Although I didn’t run it multiple times to check for errors in folder creation etc. 
I assume you are running this code from the drawing the sheetmetal part is in?

 

I did need to download an ini file and place in a file path in order to read into the options.

 

Dim strIniFile As String
strIniFile = "C:\Inventor 2022\Tweed DXF Output Settings.ini"

Can you step through the process with a message box and find the line it fails on? Maybe even open windows and check the files are created first before close

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 7 of 13

Ralf_Krieg
Advisor
Advisor
Accepted solution

Hello

 

Can't explain it in detail, but iLogic seems to create a managed object for oMdoc but release that after rule finished. Closing the drawing invalidates oMdoc and releasing crashes. Can you try to get the referenced model in another way? I simply changed the line

 

 

oMdoc = ThisDrawing.ModelDocument

 

 

to

 

 

oMdoc = oDocument.Sheets.Item(1).DrawingViews.Item(1).ReferencedDocumentDescriptor.ReferencedDocument

 

 

and nothing crashed.

 

EDIT: Tested with Inventor 2021.3


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 8 of 13

andrew.tom.reynolds
Advocate
Advocate

Thanks @Ralf_Krieg - that worked perfectly!

0 Likes
Message 9 of 13

filippo.costantin3HZMT
Advocate
Advocate

Hi Andrew, 

 

I try your code for export DXF from Drawing. How can I modify the code if I want to export only the Active Sheet (for example the number two) and not all the sheets in the drawing?

I need also to hide some layers of sheet,; can I use this code?

 

ThisDrawing.Document.StylesManager.Layers("Titolo (ISO)").Visible = False
        ThisDrawing.Document.StylesManager.Layers("Quota (ISO)").Visible = False
        ThisDrawing.Document.StylesManager.Layers("Bordo (ISO)").Visible = False
        ThisDrawing.Document.StylesManager.Layers("Simbolo (ISO)").Visible = False

 

Thanks for your help,

 

Filippo

0 Likes
Message 10 of 13

WCrihfield
Mentor
Mentor

Hi @filippo.costantin3HZMT.  I believe all of those settings are specified within the .ini file.  That .ini file will contain all the specifications you see within the Options dialog when you export manually, and click the Options button.  It basically saves all of those settings.  Within that Options dialog you can find up to 4 external file specifications, and one of those will be this INI file itself.  Another will be to an XML file for customization.  Another will be for specifying an AutoCAD template file.  Another will allow you to specify a file to use for Line type & scale mapping.  All of those file specifications, as well as all other settings within that dialog will be present in the main INI file.  You can have multiple different INI files in needed, containing different combinations of settings, as needed for different scenarios.  And you can have multiple XML files for the customizations too, for different situations.  There is commonly 3 different XML files (1 for exporting selected faces to DXF, 2 related to exporting sheet metal flat patterns to DXF.  I don't think there is a standard one for exporting a regular IDW drawing to DXF, because they usually expect them to be exported as DWG or PDF instead.  You may want to copy one of those XML files, then customize it to suit your needs.  The XML file is where the Layer editing specifications are recorded.  By default, they usually just either delete certain default layers, or rename them.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 11 of 13

filippo.costantin3HZMT
Advocate
Advocate

Hi, thanks for your reply. About the specification of file .ini I understand. But how can I export in DXF only the active sheet of drawing with 3 sheets for examples?

0 Likes
Message 12 of 13

WCrihfield
Mentor
Mentor

I am using 2022.4.1 version right now, and on page two of the Options dialog, in the upper left corner is where you can uncheck the 'All Sheets' option, and select a specific sheet.  I think the 'active' sheet is highlighted by default, when All Sheets is turned off.  But I do not believe specific sheet selection is recorded in the INI file though, just whether the All Sheets setting is Yes or No.  Therefore I believe that when that All Sheets option is unchecked, it would just try to export whichever sheet is currently active.  I do not know of another way to specify those things differently in this situation, since the one option is the only one exposed to us when using the TranstatorAddIn to  SaveCopyAs.

The Sheet object has a DataIO property, but that can only be used to export it to DWF format, as far as I know.

WCrihfield_0-1683634187797.png 

WCrihfield_1-1683634332193.png 

INI file contents, showing where the All Sheets setting is recorded.

WCrihfield_2-1683634559761.png

 

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 13 of 13

filippo.costantin3HZMT
Advocate
Advocate

Thanks a lot!!! It work!!! Fantastic! 

0 Likes