Exporting DWG Sheets to DXF (name starting with an extra underscore !?!)

Exporting DWG Sheets to DXF (name starting with an extra underscore !?!)

CCarreiras
Mentor Mentor
995 Views
9 Replies
Message 1 of 10

Exporting DWG Sheets to DXF (name starting with an extra underscore !?!)

CCarreiras
Mentor
Mentor

HI!

 

I use the code below to export any DWG sheet to the respective DXF files.

(It uses a ini file with the dxf definitions. All the files are in attach to try them)


The DXF files will get the sheet name, but it add a prefix undercore, ex:
The dxf will be:

 

_Sheet_1.dxf      instead of        Sheet_1.dxf

 

I would like to remove the initial underscore, i don't even understand why it appears.
Some help, please!! Thank you!

' 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 = 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 = ThisDoc.WorkspacePath() & "\INI_FILES\dxfout.ini"
' Create the name-value that specifies the ini file to use.
oOptions.Value("Export_Acad_IniFile") = strIniFile
End If
'Set the destination file name
SavePath=ThisDoc.WorkspacePath()&"\Z_CUT\"
oDataMedium.FileName = SavePath &".dxf"
'Publish document.
DXFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)

 

CCarreiras

EESignature

0 Likes
Accepted solutions (1)
996 Views
9 Replies
Replies (9)
Message 2 of 10

WCrihfield
Mentor
Mentor

Hi @CCarreiras.  In these two lines of code, there is only path and file extension being specified, but no main file name.

SavePath=ThisDoc.WorkspacePath()&"\Z_CUT\"
oDataMedium.FileName = SavePath &".dxf"

I assume that is on purpose, but I believe the underscore is simply added automatically by the TranslatorAddIn's source code, behind the scenes.  I am not sure if there is a way to avoid that.  It seems like there are a couple process folks have used as a workaround though.  One process was to rename the resulting files after the SaveAs process is complete (not that desirable, but not too complicated to write the code for).  It seems like the other process was to use a temporary drawing file, and copy each sheet to the other drawing (as its only sheet), then export that temporary drawing each time.  But I can't remember if that eliminated the _SheetName part being added, or if that was just used to get separate PDF's of each sheet.  I never used that second process for exporting DXF's though, so I may be remembering wrong.  It seems to me like if you manually export a single sheet drawing to a DXF, it will not add the _SheetName portion to the file name, so maybe that automation process will also work.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 10

WCrihfield
Mentor
Mentor

Hi @CCarreiras.  I was not able to download or use your attached ZIP file, due to corporate security policy, but I did copy your code to an iLogic rule editor window, then attempt to convert it to use that second process I mentioned above.  I have not tested it yet though.  It essentially creates the temporary additional drawing in the background, then near the end it loops through each sheet of the initially active drawing, copies the sheet to the temp drawing, deletes all other sheets from the temp drawing, then exports that temp drawing to DXF.  Once the loop is finished, it closes the temp drawing.  You will either need to change the path/name of the drawing template file, or else do not specify a template drawing, since that is an optional input when creating a new one.

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then Return
' 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 DrawingDocument
oDocument = ThisApplication.ActiveDocument
Dim sTPath As String = ThisApplication.DesignProjectManager.ActiveDesignProject.TemplatesPath
Dim sTemplate As String = sTPath & "\MyDrawingTemplate.dwg" '<<< CHANGE THIS >>>
Dim oTempDoc As DrawingDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject, sTemplate, False)
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
Dim strIniFile As String
strIniFile = ThisDoc.WorkspacePath() & "\INI_FILES\dxfout.ini"
' Check whether the translator has 'SaveCopyAs' options
If DXFAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
	' Create the name-value that specifies the ini file to use.
	oOptions.Value("Export_Acad_IniFile") = strIniFile
End If
'Set the destination file name
SavePath = ThisDoc.WorkspacePath & "\Z_CUT\"
For Each oSheet As Inventor.Sheet In oDocument.Sheets
	Dim oCopiedSheet As Inventor.Sheet = oSheet.CopyTo(oTempDoc)
	oTempDoc.DisplayName = oSheet.Name
	For Each oTempSheet As Sheet In oTempDoc.Sheets
		If oTempSheet IsNot oCopiedSheet Then oTempSheet.Delete
	Next 'oTempSheet
	oDataMedium.FileName = SavePath & oSheet.Name & ".dxf"
	'Publish document.
	Try
		DXFAddIn.SaveCopyAs(oTempDoc, oContext, oOptions, oDataMedium)
	Catch oEx As Exception
		Logger.Info("Error exporting " & oSheet.Name & " to DXF file." _
		& vbCrLf & oEx.Message & vbCrLf & oEx.StackTrace)
	End Try
Next 'oSheet
oTempDoc.Close(True) 'True = skip save

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 10

CCarreiras
Mentor
Mentor

Here's the files:

CCarreiras

EESignature

0 Likes
Message 5 of 10

CCarreiras
Mentor
Mentor

.... more files

 

This is the folder system:

CCarreiras_0-1698141928360.png

 

The rule is on the dwg

Now The result is:

CCarreiras_1-1698141993862.png

I would like to be:

CCarreiras_2-1698142049156.png

 

CCarreiras

EESignature

0 Likes
Message 6 of 10

CCarreiras
Mentor
Mentor

HI @WCrihfield ,
Thank you for your time.

 

FIRST

I added a clean file as template:

CCarreiras_0-1698147007584.png

When i run the rule, there's an error in Line 10.

 

 

SECOND
After this, i commented the "template lines" to avoid the error

 

and now there's an error in Line 30...

 

CCarreiras_1-1698147347689.png

 

CCarreiras

EESignature

0 Likes
Message 7 of 10

WCrihfield
Mentor
Mentor

Hi @CCarreiras.  It took me a while to set up that parent folder, sub folders, copy an INI file to the right place, and rename it, download and place your files into it, create that template DWG to place in there, create a new Project file with that name and put it in that location, then close everything to set that project as the active one...but I finally got all that done.  Then I modified the internal iLogic rule similar to the previous example, but with a couple differences.  But I am still not getting the result we want yet.  When I run the rule, there are no errors, and the entries created within the iLogic Log window indicate that the resulting new DXF files were created, and named as I wanted them to be.  But when I look in that Z_CUT folder, there is just one file in there named "SHEET", its file type is just "File", and it has zero file size.  Not quite sure what is going on there.  May be a timing related issue (not enough time for processing everything between loops), but I can't be sure yet.

Attached is the text file for that internal iLogic rule named "Export - DXF".  And the following text is what I am getting in my iLogic Log window (with the file paths truncated).

INFO| 2: >>---------------------------
INFO|DXFAddIn.DisplayName = Translator: DXF
INFO|HasSaveCopyAsOptions = True
INFO|DXFFile = ...\DXF_TEST\Z_CUT\Sheet:1.dxf
INFO|Exported DWG to following DXF file:
...\DXF_TEST\Z_CUT\Sheet:1.dxf
INFO|DXFFile = ...\DXF_TEST\Z_CUT\Sheet:2.dxf
INFO|Exported DWG to following DXF file:
...\DXF_TEST\Z_CUT\Sheet:2.dxf

 

I may be able to work on this more later, but I've got to get some stuff done at work right now.  Another tactic that we could mess around with, instead of trying to use another temporary drawing document in this process, is to try changing the Sheet.ExcludeFromCount and / or Sheet.ExcludeFromPrinting property values in an attempt to control which ones will be included in the output DXF file each time.  I'm pretty sure that if your drawing only has one sheet, it will just use the name of the drawing itself (or whatever file name you supply to it each time, such as the name of the sheet), instead of including that _SheetName at the end.  Maybe you will have better luck with the testing than I did, because my settings may all be different than yours.  I use one main project file for everything, all the time, our DesignData folder, CC folders, and and all our CAD files are stored on a network drive, and we only use IDW drawing file types in all our documentation also.  Just so you know about some of the differences I was attempting to adjust for.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 10

CCarreiras
Mentor
Mentor

Yes, the same result as you....

 

CCarreiras_0-1698157001706.png

 

CCarreiras

EESignature

0 Likes
Message 9 of 10

WCrihfield
Mentor
Mentor

Hi @CCarreiras.  While working on something else, another thought about this case came to mind.  Maybe try renaming each of the sheets in the original drawing to unique names (before the ":1" portion) before the export process.  The ":1" portion is added and managed automatically by Inventor, so we can not change that.  If you change the order of the sheets, those trailing Index numbers will not move with the sheets, they will stay in order (top sheet's name will end with ":1" no matter what).  And since the default base name "Sheet" is the same for every sheet, that is most likely messing with the process of copying that sheet to the other temp drawing, because if it was "Sheet:3" in the original document, it will always be "Sheet:1" in the temp drawing.  Since each resulting DXF file is expected to be named like the sheet, it would then be trying to export to the same file each time.  I am just guessing that if the portion of each sheets name, before the ":" symbol was unique, that may help the process succeed.  Just a thought at this point though.  Still busy on a project at work.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 10 of 10

CCarreiras
Mentor
Mentor
Accepted solution

Hi @WCrihfield, Thank you for your time and ideas.

 

Since the underscore is added by the naming process, i though in other process, to keep it simple and without messing with the conversion process.

The process is: after creating the conversion files (dxf), simple remove the first character in all files present in that specific folder, so, i added the code below to the end of the original code in it works like a charm:

 'Iterate through the files And Rename them.
        For Each File As String In files
            Dim fileInfo As New System.IO.FileInfo(File)
            Dim newName As String = System.IO.Path.Combine(SavePath, fileInfo.Name.Substring(1))
            System.IO.File.Move(File, newName)
        Next

 

CCarreiras

EESignature