Export Sketch DXF with Sequential Numbering

Export Sketch DXF with Sequential Numbering

luken_troy
Explorer Explorer
348 Views
2 Replies
Message 1 of 3

Export Sketch DXF with Sequential Numbering

luken_troy
Explorer
Explorer

Hey All,

 

I am fairly new to iLogic and the possibilities it possesses. So, the goal with this code is to export a sketch from an Inventor Part, to dxf format, to a folder. I have the code written for that from a different post. What I need is to do have sequential numbers. Preferably to check the folder to see what the last number used was then to add the new number to the newest export. Hoping to keep it simple but I feel like this certainly adds some complexity to it. I have been searching for an answer for a while now and can not find anything. Hoping you guys have some answers.

 

At the very least having the number input as a message box would not be the end of the world.

 

Current file name is ExportSketchGW. Would like it to preferably be ExportSketchGW1, ExportSketchGW2, ExportSketchGW3, etc.

 

Current Code is:

Dim fileName As String = "I:\Inventor Drawings\Export Sketches - Overlay\ExportSketchGW.dxf"
Dim doc As PartDocument = ThisDoc.Document
Dim sketch As PlanarSketch = doc.ComponentDefinition.Sketches.Item("GW CHECK")

Dim sOut As String = "DXF"
sketch.DataIO.WriteDataToFile(sOut, fileName)

 

Thanks in advance for any help offered!!

 

Troy L.

 

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

WCrihfield
Mentor
Mentor
Accepted solution

Hi @luken_troy.  That is a fairly common type of request, but can also be fairly complex to achieve, depending on the situation.  I created an iLogic rule that you can try out though.  The most difficult part of course is checking to see what the next file name suffix increment should be.  I put that task into a separate Function block of code, because you might want to use something like that in other places too.  It will search within the specified directory for part files (using the supplied file extension as the filter).  Then it filters through their file names for the ones that start with the specified 'base file name', and attempts to capture the rest of the file name after that specified text, in an attempt to get the Integer (whole number, could be multiple digits) at the end, while avoiding the file's extension.  Then, if that remaining text is numerical, it tries to convert it from a String to an Integer, so we can compare it mathematically with other Integer type values to get the highest one found.  Then it will return that value to the 'main' code, where we will add 1 to it (because it finds the last/highest number already existing).   The rest of the code is fairly simple to follow.

Sub Main
	Dim sPath As String = "I:\Inventor Drawings\Export Sketches - Overlay\"
	Dim sBaseFileName As String = "ExportSketchGW"
	Dim sExt As String = ".dxf"
	Dim iNum As Integer = GetLastFileIncrement(sPath, sBaseFileName, sExt) + 1
	Dim sNewFullFileName As String = sPath & sBaseFileName & iNum.ToString & sExt
	Dim doc As PartDocument = ThisDoc.Document
	Dim sketch As PlanarSketch = doc.ComponentDefinition.Sketches.Item("GW CHECK")
	Dim sOut As String = "DXF"
	sketch.DataIO.WriteDataToFile(sOut, sNewFullFileName)
End Sub

Function GetLastFileIncrement(sFolder As String, sBaseFileName As String, sExtension As String) As Integer
	If sFolder = "" Or sBaseFileName = "" Or sExtension = "" Then Return 0
	If System.IO.Directory.Exists(sFolder) = False Then Return 0
	Dim oFiles() As String = System.IO.Directory.GetFiles(sFolder, "*" & sExtension, System.IO.SearchOption.TopDirectoryOnly)
	If oFiles Is Nothing OrElse oFiles.Length = 0 Then Return 0
	Dim HighestNum As Integer = 0
	For Each sFile In oFiles
		Dim sFileName As String = System.IO.Path.GetFileNameWithoutExtension(sFile)
		If sFileName.StartsWith(sBaseFileName) = False Then Continue For
		Dim sSuffix As String = sFileName.Replace(sBaseFileName, "")
		If sSuffix = "" Then Continue For
		If IsNumeric(sSuffix) Then
			Dim Num As Integer = 0
			Try : Num = Convert.ToInt32(sSuffix) : Catch : End Try
			If Num = 0 Then Continue For
			If HighestNum = 0 Then HighestNum = Num
			If Num > HighestNum Then HighestNum = Num
		End If
	Next 'sFile
	Return HighestNum
End Function

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 3

luken_troy
Explorer
Explorer

Hey @WCrihfield . Thank you, that works very well. Definitely will use this, it saves a good chunk of time and makes it simpler in AutoCAD. Really appreciate your input and guidance. And you are correct the rest of the code is easy to follow. I was just having a tough time figuring out the order of things and what else I needed to include. Thanks again!

0 Likes