Stop creating dxf when file already exists

Stop creating dxf when file already exists

luc_reinders
Participant Participant
391 Views
2 Replies
Message 1 of 3

Stop creating dxf when file already exists

luc_reinders
Participant
Participant

Hello,

 

I have a rule that creates a dxf from a face in a sheet metal part. When I run the rule, but there is already a dxf from the part it still overwrites the dxf. I want to prevent to overwrite an existing dxf file. 

Is there a simple code to stop the action/rule when there is already a dxf file of the part?

 

This is the rule:

'check that this active document is a part file 
If ThisApplication.ActiveDocument.DocumentType <> kPartDocumentObject Then
MessageBox.Show ("Please open a part document", "iLogic")
End If
''Set Variables
'Set a reference to the active document
Dim oDoc As PartDocument
	oDoc = ThisApplication.ActiveDocument
	oQty = iProperties.Value(docFName, "Project", "Stock Number")
	oMat = iProperties.Value(docFName, "Summary", "Comments")
	oFolder = ThisDoc.Path & "\"
	oFileName = ThisDoc.FileName(False) & oMat & oQty & ".dxf" 'without extension
'add filename to memory for file save dialog
Dim Cm As CommandManager
	Cm = ThisApplication.CommandManager
	Cm.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, oFolder & oFileName )
Dim oSelectSet As SelectSet
	oSelectSet = oDoc.SelectSet
oDoc.SelectSet.Clear()


'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

'SELECTION CODE
Dim oFace As Face 
oFace = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select a face") 
If (oFace Is Nothing) Then
Return
Else
Call oDoc.SelectSet.Select(oFace)
End If

' EXPORT CODE 1
Dim oCtrlDef As ButtonDefinition
oCtrlDef = ThisApplication.CommandManager.ControlDefinitions.Item("GeomToDXFCommand")
Call oCtrlDef.Execute


'CLEAR selectset
oDoc.SelectSet.Clear()

 Thanks for the help,

 

Luc

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

WCrihfield
Mentor
Mentor
Accepted solution

It's a bit harder to control when you are simply executing a command to create the DXF file.  But basically, you should be checking for that oFolder then also for the (oFolder & oFileName) both before you use the PostPrivateEvent line, to make sure the folder exists, and then check to see if that file already exists.  If the file already exists, then exit the rule, instead of going ahead with the rest of the code.  Or change the file name as needed before including it in the PostPrivateEvent line, so it will write it to a new file, instead of overwriting the existing one.  Checking if a file exists is about the same as checking if the folder exists.  When checking if a file exists, you need to include the full file name, not just the file name.

If System.IO.File.Exists(oFolder & oFileName) Then
	MsgBox("That file already exists.  Exiting the rule.",,"")
	Exit Sub
	'or change oFileName value
End If

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 3

luc_reinders
Participant
Participant

Hello @WCrihfield 

 

Thank you for the explanation and thank you for your help!

With that part of the code I managed to change the rule to how I wanted it to work.

 

 

0 Likes