iLogic Rule to save STEP file from drawing - inconsistent results

iLogic Rule to save STEP file from drawing - inconsistent results

steve_lindley
Advocate Advocate
1,687 Views
12 Replies
Message 1 of 13

iLogic Rule to save STEP file from drawing - inconsistent results

steve_lindley
Advocate
Advocate

Our process here is to create a STEP file at the same time we create dxf's and pdfs of our drawings, to pass on to our suppliers. We don't have Vault, so to speed things up, I've created a rule. (I can't take all the credit - most of this has been robbed from other posts on here). However, we only create STEP files of folded sheet metal items, so I have a separate rule just for those.

 

You'll see below, that the rule opens the part from the base view on the drawing. It then saves as a STEP file to a location and then closes the part. 

 

My rule seems to work most of the time, but it can be sporadic. For instance, today, I've opened around 40 drawings at once and started to create the files and the STEP rule has been fine for the first 16 files that needed it and then I've started to see problems. What happens is the rule runs, without giving me an error. The part opens from the drawing and I get my message to say it has been saved (although this is superficial). However, I've got no STEP file in the folder. Since some have saved, it's not a location issue. 

 

I thought maybe it was all down to the type of base view. Some of my colleagues place a flat pattern view first, rather than the folded. However, on some I've had issue with today, this has not been the case. 

 

Could it be the quantity of files I'm opening at once? Not sure if that could have an impact. I'll try closing all and starting again to see what difference it makes.

 

Could it just be an inefficient rule? Can anyone see any glaring errors in my code?

 

Could it be the speed that the rules run at? A separate issue that I see on another rule is where it doesn't have enough time to process correctly and I have to run it twice to get the result. Could this be happening here? I haven't been able to find a snippet of code for adding a pause anywhere, so I'd be grateful if anyone could point me at some please, just to rule this out.

 

SyntaxEditor Code Snippet

'Dim statement: Declares and allocates storage space for variables
Dim oPart As Inventor.PartDocument

'Takes the part shown in the 1st view
'of the current sheet's DrawingViews collection
oDrawingDoc = ThisDrawing.Document
oSheet = ActiveSheet.Sheet
oDrawingView = oSheet.DrawingViews(1)
oPart = ActiveSheet.View(oDrawingView.Name).ModelDocument

'Activate part for creating STEP file
NewFileName = ThisDoc.ChangeExtension(".ipt")
ThisApplication.Documents.Open(NewFileName, True)
    

If oPart.DocumentType = kPartDocumentObject Then 'Confirm part
	If oPart.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
		
   		' Get the STEP translator Add-In.
		Dim oSTEPTranslator As TranslatorAddIn
		oSTEPTranslator = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
		Dim oContext As TranslationContext
		oContext = ThisApplication.TransientObjects.CreateTranslationContext
		Dim oOptions As NameValueMap
		oOptions = ThisApplication.TransientObjects.CreateNameValueMap
		
		oStepFileName = iProperties.Value("Project", "Part Number")
		
If oSTEPTranslator.HasSaveCopyAsOptions(ThisApplication.ActiveDocument, oContext, oOptions) Then
    ' Set application protocol.
    ' 2 = AP 203 - Configuration Controlled Design
    ' 3 = AP 214 - Automotive Design
    oOptions.Value("ApplicationProtocolType") = 2
    ' Other options...
    'oOptions.Value("Author") = ""
    'oOptions.Value("Authorization") = ""
    'oOptions.Value("Description") = ""
    'oOptions.Value("Organization") = ""
    oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
    Dim oData As DataMedium
    oData = ThisApplication.TransientObjects.CreateDataMedium
	strFolder = "R:\3D Data\DXF\Design Office dxfs - Steves\"
    oData.FileName = strFolder & "\" & oStepFileName & ".stp"
    oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oData)
End If


		
	Else 'Confirm Standard
	MessageBox.Show("Not a sheet metal part - No STEP file saved", "Check: Is this sheet metal?")
	End If

End If

	ThisApplication.CommandManager.ControlDefinitions.Item("AppFileCloseCmd").Execute
'Gives Feedback to User

MessageBox.Show("STEP format saved", "File Save")

 

 

I look forward to hearing what you all think...

Many thanks

Steve

Running Inventor Pro 2019.1.2 

0 Likes
1,688 Views
12 Replies
Replies (12)
Message 2 of 13

JamieVJohnson2
Collaborator
Collaborator

The silent killer looks only to be your first and third if statements.  They both have no else response, and as such would just pass right over silently without error if the condition was false.  The first one seems off base to me.

If oPart.DocumentType = kPartDocument then...  

Here's why, you define oPart as Inventor.Document.  So it will be that type or fail to take its assignment.  However, more cleanly, you want to run this:

If ActiveSheet.View(oDrawingView.Name).ModelDocument.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then

oPart = ActiveSheet.View(oDrawingView.Name).ModelDocument

...

Next the Translator .HasSaveCopyAsOptions() has no feedback to the developer/user if it doesn't.

To find the issue, when executing you could utilize the .Net System.IO namespace and check if file exists

oStepTranslator.SaveCopyAs(…)

dim fInfo as new system.IO.FileInfo(strFolder)

if fInfo.Exists = false then

'tell the user something went wrong

end if

 

My guess is, there is a subtle difference in Inventor's interpretation of your if statements, that makes it fail, but we can dive deeper if you find there is a memory management issue with the translator.

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
Message 3 of 13

steve_lindley
Advocate
Advocate

@JamieVJohnson2Thanks for taking the time to look at this for me. I really appreciate it. I am a novice at this, so although I see what you mean about the missing ELSE statements and I think I understand what you're suggesting with the document type and the checking, I'm afraid I don't really understand the code. I did swap it all out though and tried with drawings that failed yesterday.

 

What happened, emphasises how out of my depth I am with this! To give a little more background of what I'm doing;

I'm running through a load of drawings that all start with file names 3520-543- and end with consequetive suffixes, i.e. 01.dwg, 02.dwg, 03.dwg, etc. The last one is 3520-543-36.dwg. Yesterday I started at 36 and was working through to 01. I got to about 18 before the STEP files stopped being created. So this morning, I thought I'd test from 18 to 01. When I run the rule on 18, it opens the part, saves a STEP file and closes as expected. No error messages and the STEP file appears in the folder but now I see it has changed the file name. Instead of being 3520-543-18.stp it is now 3520-543-37.stp. As I said, the last ipt file is 36, so I assumed it was just taking the next available number for some reason.

 

If I open the stp file in Inventor, I can see it has saved the correct part, but for some reason it has renumbered it during save.

 

I tried running the rule on 3520-543-17. It saved the STEP as 3520-543-36. This is even more confusing to me because now it's using numbers of other parts, but it gets worse. The next drawing of a folded part (which are the only ones I run the rule on) is 13, so I've closed the drawings in between and run the rule on 13. It saves as 3520-543-11.stp. Not even a consequetive number or in line with a decreasing pattern of file names.

 

It saved 12 as 10. 11 as 09. And then 10 appeared not to save a new STEP file at all. So, I opened 3520-543-10.stp (which was created when I ran the rule on 12) and it matches 10.ipt, so it is overwriting the .stp file from 12. I tried again with my original code (since I'd saved a copy). The exact same thing happens and is clearly what has been happening all along. Same numbers even.

 

If I open the part without using the rule and save as a STEP file in the conventional way, I've got no problems. The file name stays as it should.

 

I thought then, maybe the part has been renumbered at some point in the past, so I checked the iproperties of the parts and they're all correct.

 

I'm absolutely baffled. If you can make sense of any of this, I'd be more than happy to listen.

 

Many thanks

 

Steve

0 Likes
Message 4 of 13

JamieVJohnson2
Collaborator
Collaborator

Here is a new feature that will really help your debugging"

iLogic Logging....  It allows you to write yourself messages in line that popup in a window while the code is running.  Use this to message yourself the file name, the variables values expected, the locations in the code where the processing is happening.  This can be more useful than the missing else statements, or work in conjunction with them.


http://help.autodesk.com/view/INVNTOR/2019/ENU/?guid=GUID-FF37C1D2-A7D4-4F9E-A5C5-BA8E517A78C4

 

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
Message 5 of 13

JamieVJohnson2
Collaborator
Collaborator

Look at this line:

oStepFileName = iProperties.Value("Project", "Part Number")

 

iProperties of what?  iProperties of current document, of active document, of document a,b,c… I think here is your issue.  You are leaving it up to Inventor to determine what the current object is for iProperties.  Try specifying the document.iProperties.value (or using VB.Net document.propertysets(item).properties(item).value)

Here are my VB.Net routines for getting an iProperty notice I always have to supply the document in question:

    Public Function FindiProperty(ByVal PropSetName As String, ByVal PropName As String, ByVal invDoc As Inventor.Document) As Inventor.Property
        For Each propSet As PropertySet In invDoc.PropertySets
            If propSet.Name = PropSetName OrElse propSet.InternalName = PropSetName Then
                For Each prop As Inventor.Property In propSet
                    If prop.Name.Equals(PropName) Then Return prop
                Next
                Exit For
            End If
        Next
        Return Nothing
    End Function

    Public Function GetiPropertyValue(PropSetName As String, PropName As String, invDoc As Inventor.Document) As Object
        Dim prop As Inventor.Property = FindiProperty(PropSetName, PropName, invDoc)
        If prop IsNot Nothing Then Return prop.Value
        Return Nothing
    End Function
Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
Message 6 of 13

steve_lindley
Advocate
Advocate

Forgive my naivety. Again, I understand what your saying, but I'm not at a level to really understand the coding.

 

I had a look at the new logging feature and it sounds like it can help but I can't figure out how to get it to include the variables in the message, to help me see where it's going wrong. I've even tried just including message boxes in my code, but I'm not sure how to get the right information to show. It's obviously got to be a string in order to include it in the message, but can't put my finger on which string variable can tell me what the active object is.

 

I also tried to copy and paste your code in, to try to get the iproperties of the active object. I don't know whether just copying and pasting was the right thing to do, but I get the error message "The rule must contain Sub Main ()... End Sub". I haven't yet learned how and when I need to use Sub Main. Microsoft's help on VB suggests adding

Module mainModule  
    Sub Main()

and

 End Sub  
End Module

But I get the same error message.

What am I missing, other than years of experience with programming!?!

Thanks again for your patience and help

 

Steve

 

0 Likes
Message 7 of 13

JamieVJohnson2
Collaborator
Collaborator

In iLogic, it wants to run either "Sub Main" or the 'only sub available'.  This means, if you have a page of code (module) aka an iLogic rule, and you grouped routines into separate functions (or subs aka a function that returns nothing), THEN you must use "Sub Main" and within that sub, you must declare what function (or functions) to run and in what order, the order you listed them (totally made up code, don't try to run it):

 

'beginning of iLogic Rule (aka module) page

 

Sub Main

   MyAwesomeRoutine()

End Sub

 

Public Sub MyAwesomeRoutine()

   MyHelperRoutine1(myData)

   Dim newObject as OutputObjectType = MyHelperRoutine2(myOtherData)

End Sub

 

Public Sub MyHelperRoutine1 (some useful input data)

  Do something with usefuldata

End Sub

 

Public Function MyHelperRoutine2(some other useful data) as OutputObjectType

   Dim somethingElse as outputobjecttype = nothing

   somethineElse = Do something with usefuldata

   Return somethingelse

End Function

 

'end of iLogic Rule page

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
Message 8 of 13

steve_lindley
Advocate
Advocate

@JamieVJohnson2Thanks for all your help. I've taken everything on board that you've suggested. Unfortunately, I can't get it to work and I'm running out of time to spend on it. At the minute, I've seperated out the code into sub routines and functions but I get about 12 errors. Mostly 'Arguement not specified' relating to those parameters in the public functions you gave me but a few of variables 'not declared' too and I'm assuming it's because I've seperated things out.

 

For now (I do plan to come back to it at some point), I have included a message box that displays the name that has been saved, so that the user can immediately see if it matches or if it's different. If it's different, they'll have to create the STEP file themselves.

 

The issue is only in certain parts it seems. Perhaps where they have been renamed in the past. But the same issue is seen on different colleagues machines, so it's clearly in those parts and nothing else. For the few parts affected, they are the minority thankfully.

0 Likes
Message 9 of 13

bretrick30
Advocate
Advocate

@steve_lindley  I made some changes to your code and it is working for me.  You don't need to open the part document separately because it is already "open" since it is in the drawing.  If you did want to open it separately, I changed the line where you were getting the file name for that part but left it commented out just so you can see how I did it.

 

I replaced and commented out the line on the bottom that was using the Command Manager to close the open part file, since it is not opening the part file.  If you were to open that document, a better way to close it is to just use the reference to its object that you created, oPart.  The command manager method you used works but it will close the document that is currently active.  That works fine with this rule, but if for some reason in a different rule you are opening and activating multiple documents, closing using the object references gives you more control. 

 

I also moved your user feedback stating that the STEP format was saved to be inside the if statement so that it is not displayed no matter if the file is a sheet metal document or not.

 

Another thing I added, just so you can verify that the STEP file was actually created is an if statement checking to see if the new file exists in the location.

 

 

SyntaxEditor Code Snippet

'Dim statement: Declares and allocates storage space for variables
Dim oPart As Inventor.PartDocument

'Takes the part shown in the 1st view
'of the current sheet's DrawingViews collection
oDrawingDoc = ThisDrawing.Document
oSheet = ActiveSheet.Sheet
oDrawingView = oSheet.DrawingViews.Item(1)

oPart = ActiveSheet.View(oDrawingView.Name).ModelDocument


'Activate part for creating STEP file
'NewFileName = oPart.FullFileName
'ThisApplication.Documents.Open(NewFileName, True)


If oPart.DocumentType = kPartDocumentObject Then 'Confirm part
	If oPart.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
		
   		' Get the STEP translator Add-In.
		Dim oSTEPTranslator As TranslatorAddIn
		oSTEPTranslator = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
		Dim oContext As TranslationContext
		oContext = ThisApplication.TransientObjects.CreateTranslationContext
		Dim oOptions As NameValueMap
		oOptions = ThisApplication.TransientObjects.CreateNameValueMap
		
		oStepFileName = iProperties.Value("Project", "Part Number")
		
		If oSTEPTranslator.HasSaveCopyAsOptions(ThisApplication.ActiveDocument, oContext, oOptions) Then
		    ' Set application protocol.
		    ' 2 = AP 203 - Configuration Controlled Design
		    ' 3 = AP 214 - Automotive Design
		    oOptions.Value("ApplicationProtocolType") = 2
		    ' Other options...
		    'oOptions.Value("Author") = ""
		    'oOptions.Value("Authorization") = ""
		    'oOptions.Value("Description") = ""
		    'oOptions.Value("Organization") = ""
		    oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
		    Dim oData As DataMedium
		    oData = ThisApplication.TransientObjects.CreateDataMedium
			strFolder = "R:\3D Data\DXF\Design Office dxfs - Steves\"
		    oData.FileName = strFolder & "\" & oStepFileName & ".stp"
		    oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oData)
		End If
		
		'Gives Feedback to User
		
		If System.IO.File.Exists(strFolder & "\" & oStepFileName & ".stp") Then
			MessageBox.Show("STEP format saved", "File Save")
		Else
			MessageBox.Show("STEP not saved")
		End If
		
	Else 'Confirm Standard
	MessageBox.Show("Not a sheet metal part - No STEP file saved", "Check: Is this sheet metal?")
	End If

'oPart.Close()
End If

 

Message 10 of 13

steve_lindley
Advocate
Advocate

@bretrick30Thanks for your effort in trying to solve this for me.

 

I copied your version directly into a new rule and gave it a try. It does indeed create a STEP file, but as with my previous versions of this code, with certain parts, (that I suspect have been renamed in the past using Design Assistant), it gives them a different file name to the part. If you've been able to follow my ramblings on this thread, I think this has been the problem all along. I tried your rule on a drawing called 3520-543-18.dwg, which has views of a part called 3520-543-18.ipt. Yet the STEP file saved is 3520-543-37.stp.

 

I believe @JamieVJohnson2 has the right idea that the line:

SyntaxEditor Code Snippet

oStepFileName = iProperties.Value("Project", "Part Number")

isn't clear enough about which iproperties it is using. However, I've been unable to translate this into my code. 

 

I do wonder if the part files have been corrupted somehow during the re-naming process and somewhere in the part, it's holding onto the old number, but I've got absolutely no idea how to go about cleaning that up!

0 Likes
Message 11 of 13

JamieVJohnson2
Collaborator
Collaborator

Looks like you need something to reliably generate the part number that you can then force into the iProperty Part Number.  For us that was the file name.  Our file names matched the part number, any deviation was cause for alarm.  You can use system.IO (path and fileinfo) to get filename without extension (I think its built into Inventor as well), then use that text in a variable to set/reset your part number iProperty.  If the files are kept in vault, use the historical version to identify part numbers that once were.

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
0 Likes
Message 12 of 13

maarten_desmet
Participant
Participant

Hello,

 

If I copy the routine, I only get a .stp file from the .idw.

I want it from the .ipt that's inside that specific .idw.. want went wrong?

 

Can I explain what I want?

 

I want a routine that follows these steps

1. Open .idw (manually)

2. Check the .ipt that's in that .idw

3. Open .ipt in the background

4. Save .ipt as .stp (still in background) using the .idw name.

    Location -> Same as where the .idw is saved.

5. Close .ipt in background.

 

Can someone help me with this?

 

Thanks in advance!

0 Likes
Message 13 of 13

steve_lindley
Advocate
Advocate

@maarten_desmet It seems that you want the same routine as us, so I'm happy to re-paste what we currently use.  Please see below. You'll have to change out the file path to where you want it saving. We consistently save our STEP files to the same folder, which is not where the .dwg is saved. I must give a word of caution though. The problem we have here, which I never did get to the bottom of, is that our Step files save using the Part Number iProperty from the .dwg and not from the ipt. So we do get a copy of the ipt, which is what we want, but we have to be careful that the iProperties of the dwg and ipt match. Mostly, this is always true, but in some instances, when parts have been renamed after the drawing was created, the drawing iProperties do not get updated. Hence, we have a message to ask us to check what filename we have just saved. If we notice a discrepancy, we simply delete the new step file, correct the iproperties in the dwg and re-run the rule. I hope this helps you progress.

 

  'Dim statement: Declares and allocates storage space for variables
Dim oPart As Inventor.PartDocument

'Takes the part shown in the 1st view
'of the current sheet's DrawingViews collection
oDrawingDoc = ThisDrawing.Document
oSheet = ActiveSheet.Sheet
oDrawingView = oSheet.DrawingViews(1)
If ActiveSheet.View(oDrawingView.Name).ModelDocument.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then
oPart = ActiveSheet.View(oDrawingView.Name).ModelDocument
End If

'Activate part for creating STEP file
NewFileName = ThisDoc.ChangeExtension(".ipt")
ThisApplication.Documents.Open(NewFileName, True)
    

If oPart.DocumentType = kPartDocumentObject Then 'Confirm part
	If oPart.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
		
   		' Get the STEP translator Add-In.
		Dim oSTEPTranslator As TranslatorAddIn
		oSTEPTranslator = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
		Dim oContext As TranslationContext
		oContext = ThisApplication.TransientObjects.CreateTranslationContext
		Dim oOptions As NameValueMap
		oOptions = ThisApplication.TransientObjects.CreateNameValueMap
		
		oStepFileName = iProperties.Value("Project", "Part Number")
		
If oSTEPTranslator.HasSaveCopyAsOptions(ThisApplication.ActiveDocument, oContext, oOptions) Then
    ' Set application protocol.
    ' 2 = AP 203 - Configuration Controlled Design
    ' 3 = AP 214 - Automotive Design
    oOptions.Value("ApplicationProtocolType") = 2
    ' Other options...
    'oOptions.Value("Author") = ""
    'oOptions.Value("Authorization") = ""
    'oOptions.Value("Description") = ""
    'oOptions.Value("Organization") = ""
    oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
    Dim oData As DataMedium
    oData = ThisApplication.TransientObjects.CreateDataMedium
	strFolder = "V:\3D Data\DXF\Design Office dxfs - Steves\"
    oData.FileName = strFolder & "\" & oStepFileName & ".stp"
	MessageBox.Show(oData.FileName, "This is the saved name - check it matches")
    oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oData)

End If


		
	Else 'Confirm Standard
	MessageBox.Show("Not a sheet metal part - No STEP file saved", "Check: Is this sheet metal?")
	End If

End If

	ThisApplication.CommandManager.ControlDefinitions.Item("AppFileCloseCmd").Execute

'Gives Feedback to User



0 Likes