Export drawing sheets to dwg with specific file names

Export drawing sheets to dwg with specific file names

Chris.Truelove
Contributor Contributor
2,839 Views
2 Replies
Message 1 of 3

Export drawing sheets to dwg with specific file names

Chris.Truelove
Contributor
Contributor

Hi

I hope someone can help.

I created an iLogic program that exports all sheets in an inventor drawing to separate pdf files with a specific file name for each sheet. The name is made up as follows 'Drawing No. Drawing type, Sheet No. and revision' it looks like this 12345-SK-01_01. When the pdf's are created only the sheet number is incremented to create a individual file name.

 

The problem I am having is that when I try to use this program to create 'dwg' versions of the drawing the file name is not being created correctly. 

What is happening is that the initial files name is produced and then 'sheet 1, 2, 3.....'is being added onto the end of the file name for however many sheets there are in the drawing. it then increments the sheet number within the file name and then continues to add 'sheet 1, 2, 3....' to each exported drawing file name.

so what I end up with is 100 drawing being created for a 10 sheet inventor drawing.

Example file naming;

12345-SK-01_01_Sheet _1.dwg

12345-SK-01_01_Sheet _2.dwg

12345-SK-01_01_Sheet _3.dwg......

 

12345-SK-02_01_Sheet _1.dwg

12345-SK-02_01_Sheet _2.dwg

12345-SK-02_01_Sheet _3.dwg.....

 

See attached images.

 

Also I added message boxes to show the generated file name and path. These do appear to be being created correctly. The _Sheet_1... is being added by something else within inventor?????......... maybe.......

 

I have added the code below.

The only difference between this code and the code to produce the pdf's is that pdf was replace with dwg and 

oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")

was replaced with.
oDwgAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")

I realise this a is a simplistic approach but I don't profess to be a programmer.

I hope someone can because I don't have much more hair left to pull out...

 

Thanks in advance.

 

Chris

 

 
 
Sub Main()
FileName = ThisDoc.FileName(True) 'with extension

FileExtension = Right(FileName, 3)

If FileExtension = "dwg" Then
	Save_As_dwg
Else
	ErrorMessage
End If
'MessageBox.Show("Stop 1","")
End Sub

Sub Save_As_dwg
	oPath = ThisDoc.Path
	oFileName = ThisDoc.FileName(False) 'without extension
	'oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
	oDwgAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
	oDocument = ThisApplication.ActiveDocument
	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
	oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
	'Dim oPrintMgr As DrawingPrintManager = ThisDrawing.Document.PrintManager

	'Gets the Workspace Path
	WorkspacePath = ThisDoc.WorkspacePath()

	'MessageBox.Show("WorkspacePath Set")
	WorkspacePathLength = Len(WorkspacePath)

	'Gets just the Path of the file
	PathOnly = ThisDoc.Path

	'Removes the Workspace Path from FullPath
	DirectoryPath = Strings.Right(PathOnly, PathOnly.Length - WorkspacePathLength)

	'Sets the Dirctory that the dwg should be saved in
	dwgPath = ThisDoc.Path

'	oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintSheetRange
	oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintCurrentSheet

	'MessageBox.Show("Sheet Range Set")

	'Checks to see if that directory exists, if not, it is created
		Dim oName As String
		Dim oJobNo As String
		Dim oType As String
		Dim oRev As String
		
	For Each oSheet As Sheet In ThisDrawing.Document.Sheets
		oSheet.Activate

		'Generate sheet name 'oName'
		oDoc = ThisDoc.Document
		SheetNumber  = Mid(oDoc.ActiveSheet.Name, InStr(1, oDoc.ActiveSheet.Name, ":") + 1)

		'Add zero prior to Sheet No
		SheetNo = If(SheetNumber < 10, "0" + CStr(SheetNumber), CStr(SheetNumber))
		SheetNumber = SheetNumber + 1

'		oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintCurrentSheet

		oJobNo = iProperties.Value("Custom", "Job_No.")
		oType = iProperties.Value("Custom", "Drawing_type")
		oRev = iProperties.Value("Project", "Revision Number")

		'Create drawing number
		oName = (oJobNo & "-" & oType & "-" & SheetNo & "_" & oRev)

		MessageBox.Show("The generated drawing number is " & oName)
		
		dwgName = dwgPath & "\" & oName & ".dwg"
		
		MessageBox.Show("The new dwg name is " & dwgName)

		'Set the dwg target file name
		oDataMedium.FileName = dwgName
		'MessageBox.Show("File Name Set")
		oMsgAns = MsgBox("Do you want to continue the print", vbYesNo, "Colour select")
		If oMsgAns = vbNo Then
			Return
		End If	

		'Saves the dwg in the desired location
		oDwgAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
		'ThisDoc.Document.SaveAs(dwgName , True)
		'MessageBox.Show("New dwg created")
	Next

End Sub

Sub ErrorMessage
	i = MessageBox.Show("This is not a drawing file. No dwg will be created.", "Create dwg", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
End Sub

I  

0 Likes
Accepted solutions (1)
2,840 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable

@Chris.Truelove  It has something to do with .ini file.  There had been a discussion on this thread.

https://forums.autodesk.com/t5/inventor-customization/export-to-dwg-with-filename-based-on-sheetname... 

0 Likes
Message 3 of 3

Chris.Truelove
Contributor
Contributor
Accepted solution

Thanks for replying JoJo.

Yes it is. I hadn't got around to updating this post until now but I finished editing my iLogic after much searching for answers. I had come to the same conclusion after looking in the inventor help files "Who new" 🙂

Anyway I have added my updated code below with additional functionality.

When run the user is asked if they want to export all sheets. 

If 'Yes' is selected all sheets are exported to dwg with a title made up of iProperty data and the sheet number is used with a preceding zero and imbedded into the file name.

If 'No' is selected a further window pops up asking which sheet is require. The user then enters the required sheet number and that sheet is exported with the file name created in the same way as above.

With either of these options if a file already exists with the same file name the user is asked if they want to over-right the file.

 

Please see the code below. I'm sure that someone out there could make it neater but it work of what I wanted and it appears to be robust/stable.

 

Sub Main()
	'Choose Export all or Export page
	oMsgAns = MsgBox("Do you want to export all Pages", vbYesNo, "Export Stop")
	If oMsgAns = vbYes Then
		ExportAll
	ElseIf oMsgAns = vbNo Then
		ExportPage
	End If	
End Sub

Sub ExportPage
	'Set required sheet	
	mySheet = InputBox("Enter required sheet number.", "Page select", "1")


	Dim oDoc As DrawingDocument
	oDoc = ThisApplication.ActiveDocument

	Dim oSheet As Sheet
	Dim lPos As Long
	Dim sLen As Long
	Dim sSheetName As String
	Dim sSheetNumber As String
	oSheetPrint = mySheet
    ' Get the DWG translator Add-In.
    Dim DWGAddIn As TranslatorAddIn
    DWGAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC2-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 DWGAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
	
        Dim strIniFile As String
        strIniFile = "Network_Location\dwgexport.ini"
		
        ' Create the name-value that specifies the ini file to use.
        oOptions.Value("Export_Acad_IniFile") = strIniFile
    End If	

	'--Make specified sheet active--
	For Each oSheet In oDoc.Sheets
	    lPos = InStr(oSheet.Name, ":")
	    sLen = Len(oSheet.Name)
	    sSheetName = Left(oSheet.Name, lPos -1)
	    sSheetNumber = Right(oSheet.Name, sLen -lPos )
	    'compare list selection to sheet number
	    If mySheet = sSheetNumber Then
	    'activate sheet
	    oSheet.Activate
	    Else
	    End If
	Next

	'Generate files name from iProperties and sheet number
	
	'Adding preceeding zero to sheet numbers less than 10
	oPath = ThisDoc.Path
	oSheet = ThisDrawingActiveSheet
	oDoc = ThisDoc.Document
	SheetNumber  = Mid(oDoc.ActiveSheet.Name, InStr(1, oDoc.ActiveSheet.Name, ":") + 1)

	'Add zero prior to Sheet No
	SheetNo = If(SheetNumber < 10, "0" + CStr(SheetNumber), CStr(SheetNumber))
	SheetNumber = SheetNumber + 1
	
	'Grab data from iProperties
	oJobNo = iProperties.Value("Custom", "Job_No.")
	oType = iProperties.Value("Custom", "Drawing_type")
	oRev = iProperties.Value("Project", "Revision Number")

	'Create drawing number
	oName = (oJobNo & "-" & oType & "-" & SheetNo & "_" & oRev)

	'MessageBox.Show("The generated drawing number is " & oName)
	
	DWGName = oPath & "\" & oName & ".dwg"
	
	'MessageBox.Show("The new pdf name is " & PDFName)

	'Only print the active page
	oOptions.Value("Publish_All_Sheets") = 0

    'Set the destination file name
    oDataMedium.FileName = DWGName

	'Check to see if the PDF already exists, if it does, ask if you want to over write existing or not.
	If System.IO.File.Exists(oDataMedium.FileName) = True Then
		oAnswer = MsgBox("A 'dwg' file with the name " & oName & " already exists." & vbCrLf &
		"Do you want to over write it with this new one?",vbYesNo + vbQuestion + vbDefaultButton2, "'dwg' ALREADY EXISTS")
	    'Publish document.
		If oAnswer = vbYes Then
	    	Call DWGAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)	
			ElseIf oAnswer = vbNo Then 
				GoTo SkipPoint
		End If 
	End If
	Call DWGAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
	
	SkipPoint :
	'MessageBox.Show("New PDF created")
End Sub


Sub ExportAll
	' Get the DWG translator Add-In.
    Dim DWGAddIn As TranslatorAddIn
    DWGAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC2-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 DWGAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
		
        Dim strIniFile As String
        strIniFile =  "Network_Location\dwgexport.ini"
		
        ' Create the name-value that specifies the ini file to use.
        oOptions.Value("Export_Acad_IniFile") = strIniFile
    End If	

	'Set the DWG target file name
	Dim oName As String
	Dim oJobNo As String
	Dim oType As String
	Dim oRev As String
	Dim n As Integer
	oPath = ThisDoc.Path
	
	'Generate files name from iProperties and sheet number
	For Each oSheet As Sheet In ThisDrawing.Document.Sheets
		oSheet.Activate
		
		'Generate sheet name 'oName'
		oDoc = ThisDoc.Document
		SheetNumber = Mid(oDoc.ActiveSheet.Name, InStr(1, oDoc.ActiveSheet.Name, ":") + 1)

		oDocument.Sheets.Item(oDoc.ActiveSheet.Name).Activate
		
		'MessageBox.Show("The active sheet is " & oDoc.ActiveSheet.Name)
		'Add zero prior to Sheet No
		SheetNo = If(SheetNumber < 10, "0" + CStr(SheetNumber), CStr(SheetNumber))
		'MessageBox.Show("The SheetNo is " & SheetNo)

		'Grab data from iProperties
		oJobNo = iProperties.Value("Custom", "Job_No.")
		oType = iProperties.Value("Custom", "Drawing_type")
		oRev = iProperties.Value("Project", "Revision Number")

		'Create drawing number
		oName = (oJobNo & "-" & oType & "-" & SheetNo & "_" & oRev)
		
		'MessageBox.Show("File Name Set")
		DWGName = oPath & "\" & oName & ".dwg"

		'Only print the active page
		oOptions.Value("Publish_All_Sheets") = 0

	    'Set the destination file name
	    oDataMedium.FileName =DWGName

		If System.IO.File.Exists(oDataMedium.FileName) = True Then
			oAnswer = MsgBox("A 'dwg' file with the name " & oName & " already exists." & vbCrLf &
			"Do you want to over write it with this new one?",vbYesNo + vbQuestion + vbDefaultButton2, "'dwg' ALREADY EXISTS")
		    'Publish document.
			If oAnswer = vbYes Then
		    	Call DWGAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)	
				ElseIf oAnswer = vbNo Then 
					GoTo SkipPoint
			End If 
		End If
		Call DWGAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
		
		SkipPoint :
		SheetNumber = SheetNumber + 1	
	Next

End Sub

I hope this is useful to somebody else as well or at least give them a good start

 

It does use an '.ini' file that is stored in a network location the contents of which are as follows;

-------------------------------------------------------------------------

[EXPORT SELECT OPTIONS]
AUTOCAD VERSION=AutoCAD 2013
CREATE AUTOCAD MECHANICAL=No
USE TRANSMITTAL=No
USE CUSTOMIZE=No
CUSTOMIZE FILE=C:\Users\Public\Documents\Autodesk\Inventor 2021\Design Data\DWG-DXF\FlatPattern.xml
CREATE LAYER GROUP=No
PARTS ONLY=No
REPLACE SPLINE=No
CHORD TOLERANCE=0.001000
[EXPORT PROPERTIES]
SELECTED PROPERTIES=
[EXPORT DESTINATION]
SPACE=Model
SCALING=Geometry
ALL SHEETS=No
MAPPING=MapsBest
MODEL GEOMETRY ONLY=No
EXPLODE DIMENSIONS=No
SYMBOLS ARE BLOCKED=Yes
AUTOCAD TEMPLATE=
DESTINATION DXF=No
USE ACI FOR ENTITIES AND LAYERS=No
ALLOW RASTER VIEWS=No
SHOW DESTINATION PAGE=Yes
ENABLE POSTPROCESS=Yes
[EXPORT LINE TYPE & LINE SCALE]
LINE TYPE FILE=C:\Users\Public\Documents\Autodesk\Inventor 2021\COMPATIBILITY\Support\invISO.lin
Continuous=Continuous;0.
Dashed=DASHED;0.
Dashed Space=DASHED_SPACE;0.
Long Dash Dotted=LONG_DASH_DOTTED;0.
Long Dash Double Dot=LONG_DASH_DOUBLE_DOT;0.
Long Dash Triple Dot=LONG_DASH_TRIPLE_DOT;0.
Dotted=DOTTED;0.
Chain=CHAIN;0.
Double Dash Chain=DOUBLE_DASH_CHAIN;0.
Dash Double Dot=DASH_DOUBLE_DOT;0.
Dash Dot=DASH_DOT;0.
Double Dash Dot=DOUBLE_DASH_DOT;0.
Double Dash Double Dot=DOUBLE_DASH_DOUBLE_DOT;0.
Dash Triple Dot=DASH_TRIPLE_DOT;0.
Double Dash Triple Dot=DOUBLE_DASH_TRIPLE_DOT;0.

-------------------------------------------------------------------------------------------

For those that don't know just copy the text between the dathed lines into a text file and save as 'dwgexport.ini'

Save the new 'ini' file in your chosen location  and then amend the code where is says 'Network_Location\dwgexport.ini' to reflect the new location of the ini file. This needs to be done in two locations within the code.

I originally created the ini file using the built in dwg export option within inventor and saved the ini file from there so there maybe some aspects that a pequeliar to my machine.

 

I hope this helps.

 

Chris.