Hello, I have Cobbled together this script from various post on the forums a few monthes ago (thanks for all the snippets I pilfered from examples found here, Wish I could offer propper attribution to this work, but suffice it to say that is not all mine and I could not have done it alone). We have a workflow that involves sending Step files of flattened sheet metal parts to a vendor for the laser cutting of blanks that we subsequently form in house. The code that follows opens the part file in question when a drawing is checked in and initiates the unfold command, waits for user selections, finishes the command, executes a save copy as to .Stp, and then open the resulting step file for user reveiw.
This was "written" for 2013 and worked well for monthes with one minor annoyance. It would always open the step file in a second instance of Inventor instead of opening the file in the running instance. This was Less than ideal, because the it take a little while to launch the second instance, but on the whole it was a major improvment over the way we were doing it. Fast forward six months or so and we install inventor 2014. Now the script fails to open the .stp file because the windows hasn't assoiciated step files to inventor 2014. Ok, so I reassociate .stp file with 2014 on the dept machines and voila, everything is back to normal. Except on one machine were the the .stp file open in the RUNNING instance of inventor (this is cool, it's really what I wanted to happen from the beginning). All the machines have similar hardware and are runnning Win 7 Pro. What I need to know is, what is causing the different file open behavior, and How can I get ALL The machines to behave this way!
I have attached the code in question and have pasted it below as well if you do not want to download the file. Their are file path elements that are "hard coded" into the script that will require modification to run in a different network environment that ours, and the rule is run by an I trigger set in a drawing file to "After Document Save". I am gratefull in anvance for any insight that the members of this community can offer and have faith that the "better minds than mine" here will help me sort this out.
Code:
**************************************************************************************************************
Format:HTML Format Version:1.0 StartHTML: 165 EndHTML: 66261 StartFragment: 314 EndFragment: 66229 StartSelection: 314 EndSelection: 314
SyntaxEditor Code Snippet
'Show message box and get user input - bolean yes/no question=MessageBox.Show("Would you like to Check the Drawing In?", "iLogic Question",MessageBoxButtons.YesNo,MessageBoxIcon.Question)'set condition based on answer Ifquestion=vbYesThen'define and execute Vault Check in active document command synchronously (this method waits for user to complete command)DimoDefAsControlDefinitionoDef=ThisApplication.CommandManager.ControlDefinitions.​Item("VaultCheckInTop")Callodef.execute2(True)'get the drawing filepathDimDfilepathAsStringDfilepath=ThisDoc.Path'MessageBox.Show(Dfilepath, "Debug")'Define the open documentDimopenDocAsDocumentopenDoc=ThisDoc.Document'Make sure there is model file referenced in the open documentDimdocFileAsDocumentIfThisDoc.ModelDocumentIsNotNothingThendocFile=ThisDoc.ModelDocumentElseMessageBox.Show("This drawing has no model reference", "iLogic")ReturnEndIf'format model file name DimFNamePosAsLongFNamePos=InStrRev(docFile.FullFileName, "\", -1)DimdocFNameAsStringdocFName=Right(docFile.FullFileName, Len(docFile.FullFileName)-FNamePos)'MessageBox.Show(docFName, "Debug")'get the current drawing rev level and assign it to RevDimRevAsStringRev="Rev"+iProperties.Value("Project", "Revision Number")'MessageBox.Show(Rev, "Debug")'strip C:\VaultWorkingFolder and insert V:\ the append Rev to create step file name and pathDimNFNameAsStringDimFNameLong=Len(Dfilepath)-22DimdocFNameLong=Len(docFName)-4docFName=Left(docFName,docFNameLong)NFName="V:\"+Right(Dfilepath,FNameLong)+"\"+docFName+""+Rev'MessageBox.Show(NFName, "Debug")'get model name and path and launch documentmodelFullFileName=ThisDrawing.ModelDocument.FullFileNamemodelDirectoryName=IO.Path.GetDirectoryName(modelFullFileName)modelFileName=IO.Path.GetFileName(modelFullFileName)'MessageBox.Show(modelFullFileName, "modelFullFileName")'MessageBox.Show(modelDirectoryName, "modelDirectoryName")'MessageBox.Show(modelFileName, "modelFileName")ThisDoc.Launch(modelFullFileName)'define active document (.ipt file)DimoPartDocAsPartDocumentoPartDoc=ThisApplication.ActiveDocument'define and execute "convert to sheet metal" commandoDef=ThisApplication.CommandManager.ControlDefinitions.​Item("PartConvertToSheetMetalCmd")oDef.Execute'check to see if flat pattern existsDimoCompDefAsSheetMetalComponentDefinitionoCompDef=oPartDoc.ComponentDefinition'if flat pattern exists then....IfoCompDef.HasFlatPattern=FalseThen'check for preliminary rev status and alert user that step files are not exported for rev ? or *IfRev="Rev?"ThenMessageBox.Show(".stp Files Are Not Created For Prelimary Drawings", "iLogic")'define and execute close .iptoDef=ThisApplication.CommandManager.ControlDefinitions.​Item("AppFileCloseCmd")odef.executeElseIfRev="Rev*"ThenMessageBox.Show(".stp Files Are Not Created For Drawings With Pending ECR's", "iLogic")'define and execute close .iptoDef=ThisApplication.CommandManager.ControlDefinitions.​Item("AppFileCloseCmd")odef.executeElse' dispalay new .stp filepath and nameMessageBox.Show("File Name & Path "+NFName, "iLogic")' Get the STEP translator Add-In. and create STEP fileDimoSTEPTranslatorAsTranslatorAddInoSTEPTranslator=ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")DimoContextAsTranslationContextoContext=ThisApplication.TransientObjects.CreateTranslationContextDimoOptionsAsNameValueMapoOptions=ThisApplication.TransientObjects.CreateNameValueMapIfoSTEPTranslator.HasSaveCopyAsOptions(ThisApplication.ActiveDocument, oContext, oOptions)Then' Set application protocol.' 2 = AP 203 - Configuration Controlled Design' 3 = AP 214 - Automotive DesignoOptions.Value("ApplicationProtocolType")=3' Other options...'oOptions.Value("Author") = ""'oOptions.Value("Authorization") = ""'oOptions.Value("Description") = ""'oOptions.Value("Organization") = ""oContext.Type=IOMechanismEnum.kFileBrowseIOMechanismDimoDataAsDataMediumoData=ThisApplication.TransientObjects.CreateDataMediumoData.FileName=NFName&".stp"oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oData)'define and execute close .ipt fileoDef=ThisApplication.CommandManager.ControlDefinitions.​Item("AppFileCloseCmd")odef.execute'Open new STEP FileThisDoc.Launch(NFName&".stp")EndIfEndIfElse'check for preliminary rev status and alert user that step files are not exported for rev ? or *IfRev="Rev?"ThenMessageBox.Show(".stp Files Are Not Created For Prelimary Drawings", "iLogic")'define and execute close .iptoDef=ThisApplication.CommandManager.ControlDefinitions.​Item("AppFileCloseCmd")odef.executeElseIfRev="Rev*"ThenMessageBox.Show(".stp Files Are Not Created For Drawings With Pending ECR's", "iLogic")'define and execute close .iptoDef=ThisApplication.CommandManager.ControlDefinitions.​Item("AppFileCloseCmd")odef.executeElse'define and execute Unfold command synchronously (this method waits for user to complete command)oDef=ThisApplication.CommandManager.ControlDefinitions.​Item("SheetMetalUnfoldCmd")Callodef.execute2(True)'dispalay new .stp filepath and nameMessageBox.Show("File Name & Path "+NFName, "iLogic")' Get the STEP translator Add-In. and create STEP fileDimoSTEPTranslatorAsTranslatorAddInoSTEPTranslator=ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")DimoContextAsTranslationContextoContext=ThisApplication.TransientObjects.CreateTranslationContextDimoOptionsAsNameValueMapoOptions=ThisApplication.TransientObjects.CreateNameValueMapIfoSTEPTranslator.HasSaveCopyAsOptions(ThisApplication.ActiveDocument, oContext, oOptions)Then' Set application protocol.' 2 = AP 203 - Configuration Controlled Design' 3 = AP 214 - Automotive DesignoOptions.Value("ApplicationProtocolType")=3' Other options...'oOptions.Value("Author") = ""'oOptions.Value("Authorization") = ""'oOptions.Value("Description") = ""'oOptions.Value("Organization") = ""oContext.Type=IOMechanismEnum.kFileBrowseIOMechanismDimoDataAsDataMediumoData=ThisApplication.TransientObjects.CreateDataMediumoData.FileName=NFName&".stp"oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oData)'defines and executes the Undo command to remove the unfold featureoDef=ThisApplication.CommandManager.ControlDefinitions.​Item("AppUndoCmd")odef.execute'define and execute close .ipt fileoDef=ThisApplication.CommandManager.ControlDefinitions.​Item("AppFileCloseCmd")odef.execute'Open new STEP FileThisDoc.Launch(NFName&".stp")EndIfEndIfEndIfElseIfquestion=vbNoThenReturnEndIf
Solved! Go to Solution.
Solved by adam.nagy. Go to Solution.
Reposting your code in a readable format:
'Show message box and get user input - bolean yes/no question = MessageBox.Show("Would you like to Check the Drawing In?", "iLogic Question",MessageBoxButtons.YesNo,MessageBoxIcon.Question) 'set condition based on answer If question = vbYes Then 'define and execute Vault Check in active document command synchronously (this method waits for user to complete command) Dim oDef As ControlDefinition oDef = ThisApplication.CommandManager.ControlDefinitions.Item("VaultCheckInTop") Call odef.execute2 (True) 'get the drawing filepath Dim Dfilepath As String Dfilepath = ThisDoc.Path 'MessageBox.Show(Dfilepath, "Debug") 'Define the open document Dim openDoc As Document openDoc = ThisDoc.Document 'Make sure there is model file referenced in the open document Dim docFile As Document If ThisDoc.ModelDocument IsNot Nothing Then docFile = ThisDoc.ModelDocument Else MessageBox.Show("This drawing has no model reference", "iLogic") Return End If 'format model file name Dim FNamePos As Long FNamePos = InStrRev(docFile.FullFileName, "\", -1) Dim docFName As String docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos) 'MessageBox.Show(docFName, "Debug") 'get the current drawing rev level and assign it to Rev Dim Rev As String Rev = "Rev" + iProperties.Value("Project", "Revision Number") 'MessageBox.Show(Rev, "Debug") 'strip C:\VaultWorkingFolder and insert V:\ the append Rev to create step file name and path Dim NFName As String Dim FNameLong = Len(Dfilepath)-22 Dim docFNameLong = Len(docFName)-4 docFName = Left(docFName,docFNameLong) NFName = "V:\" + Right(Dfilepath,FNameLong)+"\" + docFName + " " + Rev 'MessageBox.Show(NFName, "Debug") 'get model name and path and launch document modelFullFileName = ThisDrawing.ModelDocument.FullFileName modelDirectoryName = IO.Path.GetDirectoryName(modelFullFileName) modelFileName = IO.Path.GetFileName(modelFullFileName) 'MessageBox.Show(modelFullFileName, "modelFullFileName") 'MessageBox.Show(modelDirectoryName, "modelDirectoryName") 'MessageBox.Show(modelFileName, "modelFileName") ThisDoc.Launch(modelFullFileName) 'define active document (.ipt file) Dim oPartDoc As PartDocument oPartDoc = ThisApplication.ActiveDocument 'define and execute "convert to sheet metal" command oDef = ThisApplication.CommandManager.ControlDefinitions.Item("PartConvertToSheetMetalCmd") oDef.Execute 'check to see if flat pattern exists Dim oCompDef As SheetMetalComponentDefinition oCompDef = oPartDoc.ComponentDefinition 'if flat pattern exists then.... If oCompDef.HasFlatPattern = False Then 'check for preliminary rev status and alert user that step files are not exported for rev ? or * If Rev = "Rev?" Then MessageBox.Show(".stp Files Are Not Created For Prelimary Drawings", "iLogic") 'define and execute close .ipt oDef = ThisApplication.CommandManager.ControlDefinitions.Item("AppFileCloseCmd") odef.execute ElseIf Rev = "Rev*" Then MessageBox.Show(".stp Files Are Not Created For Drawings With Pending ECR's", "iLogic") 'define and execute close .ipt oDef = ThisApplication.CommandManager.ControlDefinitions.Item("AppFileCloseCmd") odef.execute Else ' dispalay new .stp filepath and name MessageBox.Show("File Name & Path " +NFName, "iLogic") ' Get the STEP translator Add-In. and create STEP file 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 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") = 3 ' 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 oData.FileName = NFName & ".stp" oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oData) 'define and execute close .ipt file oDef = ThisApplication.CommandManager.ControlDefinitions.Item("AppFileCloseCmd") odef.execute 'Open new STEP File ThisDoc.Launch(NFName & ".stp") End If End If Else 'check for preliminary rev status and alert user that step files are not exported for rev ? or * If Rev = "Rev?" Then MessageBox.Show(".stp Files Are Not Created For Prelimary Drawings", "iLogic") 'define and execute close .ipt oDef = ThisApplication.CommandManager.ControlDefinitions.Item("AppFileCloseCmd") odef.execute ElseIf Rev = "Rev*" Then MessageBox.Show(".stp Files Are Not Created For Drawings With Pending ECR's", "iLogic") 'define and execute close .ipt oDef = ThisApplication.CommandManager.ControlDefinitions.Item("AppFileCloseCmd") odef.execute Else 'define and execute Unfold command synchronously (this method waits for user to complete command) oDef = ThisApplication.CommandManager.ControlDefinitions.Item("SheetMetalUnfoldCmd") Call odef.execute2 (True) 'dispalay new .stp filepath and name MessageBox.Show("File Name & Path " +NFName, "iLogic") ' Get the STEP translator Add-In. and create STEP file 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 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") = 3 ' 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 oData.FileName = NFName & ".stp" oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oData) 'defines and executes the Undo command to remove the unfold feature oDef = ThisApplication.CommandManager.ControlDefinitions.Item("AppUndoCmd") odef.execute 'define and execute close .ipt file oDef = ThisApplication.CommandManager.ControlDefinitions.Item("AppFileCloseCmd") odef.execute 'Open new STEP File ThisDoc.Launch(NFName & ".stp") End If End If End If Else If question = vbNo Then Return End If
Thank You for making those edits, I can Never seem to paste code and have it retain formating. Is there an FAQ or any hints that I can look at to save trouble in the furure?
Hi,
So as I understand the issue is with: ThisDoc.Launch(NFName&".stp")
I suspect you have similar issues if you simply double-click on a step file in Windows Explorer, right?
1) I ran into this article about having a similar behaviour with Inventor files, like *.ipt: http://beinginventive.typepad.com/being-inventive/2012/04/opening-inventor-file-opens-new-instance-o...
Based on that I think I found what needs to be modified in the registry in order to make things work - at least it works for me:
Added the following entries in the registry:
Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Classes\stp_auto_file] @="" [HKEY_CURRENT_USER\Software\Classes\stp_auto_file\shell] [HKEY_CURRENT_USER\Software\Classes\stp_auto_file\shell\open] [HKEY_CURRENT_USER\Software\Classes\stp_auto_file\shell\open\command] @="\"C:\\Program Files\\Autodesk\\Inventor 2014\\Bin\\Inventor.exe\" /dde" [HKEY_CURRENT_USER\Software\Classes\stp_auto_file\shell\open\ddeexec] @="[open(\"%1\")]"
2) A much simpler solution would be to use the Inventor API to open the file:
ThisApplication.Documents.Open(NFName & ".stp")
I hope this helps.
Cheers