Rule to open model from drawing then run 2nd external rule to export STP of the model not working.

Rule to open model from drawing then run 2nd external rule to export STP of the model not working.

JMatwiejczuk32LYL
Contributor Contributor
350 Views
2 Replies
Message 1 of 3

Rule to open model from drawing then run 2nd external rule to export STP of the model not working.

JMatwiejczuk32LYL
Contributor
Contributor

Hey guys I cant figure out why my imbedded rule isn't outputting any files.

 

The expected workflow is:

  1. run external rule "FO - Export STP From Drawing Rev Date" from IDW
  2. the rule then checks if the IDW is a IPT or IAM drawing
  3. sets the path to the appropriate model 
  4. opens the model
  5. run 2nd external rule "FO - STP Rev Date" from within that model to export the STP
  6. closes the model

When running the 2nd rule straight from the model it works just fine but from the drawing I'm not getting any errors and the model opens and closes but no file is created so I think that opening the model essentially does nothing.

 

Does anyone know how to make this work? Or even better is there a way to run the second rule like it is run from within the model without opening the model in the first place? rule 1 "FO - Export STP From Drawing Rev Date" below

doc = ThisDoc.ModelDocument
	
Dim oPartPath As String

If doc.DocumentType = kPartDocumentObject Then
oPartPath = ThisDoc.PathAndFileName(False) & ".ipt"
Else If doc.DocumentType = kAssemblyDocumentObject Then
oPartPath = ThisDoc.PathAndFileName(False) & ".iam"
End If 
	
' Set a reference to the target part	
oPart = ThisApplication.Documents.ItemByName(oPartPath)
' Open the target part
'ThisApplication.Documents.Open(oPartPath)
ThisDoc.Launch(oPartPath)
' Save a copy of oPart as a step file

iLogicVb.RunExternalRule("FO - STP Rev Date")

' Close oPart
oPart.Close

rule 2 "FO - STP Rev Date" Below

 

'Get Current date
Dim Time As DateTime = DateTime.Now
Dim Format As String = "yyMMdd"

'Get filename without extension
Dim oFileName As String 
oFileName = ThisDoc.FileName(False)

'set custom naming convention for output file
Dim sPartNumb As String = iProperties.Value("Project", "Part Number") & "_" & iProperties.Value("Project", "Revision Number") & "_" & Time.ToString(Format)


'Export parameters
If TypeOf ThisDoc.Document Is PartDocument Or TypeOf ThisDoc.Document Is AssemblyDocument Then
		Dim oDoc As Document = ThisDoc.Document
		Dim oSTEPTranslator As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
		Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
		Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
		oPath = ThisDoc.Path
		If oSTEPTranslator.HasSaveCopyAsOptions(oDoc, 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 = ThisApplication.TransientObjects.CreateDataMedium
			
'get Output target folder path
	oFolder = oPath & "\" & oFileName & "\"
'Check for the Outputs folder and create it if it does not exist
	If Not System.IO.Directory.Exists(oFolder) Then
    System.IO.Directory.CreateDirectory(oFolder)
	End If

		    oData.FileName = oFolder &  sPartNumb & ".stp"
			oSTEPTranslator.SaveCopyAs(oDoc, oContext, oOptions, oData)
		End If
'	End If
Else
	MessageBox.Show("Message", "Title")
end if
0 Likes
Accepted solutions (1)
351 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor

Hi @JMatwiejczuk32LYL.  I see that you are using 'ThisDoc' and 'ThisDoc.Document' in the second rule, which is what I would have suggested also.  But I think it may be possible that in this case that 'ThisDoc' reference may still be pointing to the drawing, since it was technically the 'active' document when the rule process started.  I have observed that when the 'ThisDoc' reference is used in an external rule, it will often point to whichever document was 'active' when the rule first started, even if used near the end of a rule where multiple other documents may have been opened.  The best thing to do here may be to slightly transform the start of your second rule, to prepare it for optionally 'receiving' the needed data about which document to target from the first rule.  That way it will still work independently, but will work just as good as a secondary rule.  If you are not familial with that process, it used the RuleArguments object to pass 'arguments' (data) to another rule when you use one of the RunRule("rule name", oArguments) methods.  It uses a NameValueMap where you put the data into it from the first rule, then include that when you run the other rule, then the other rule can receive that data to use when it does its thing.

 

Edit:  You could also try using ThisDoc.ModelDocument in that second rule, if a document type check indicates that it is not either a part of assembly, just as an extra layer.  Sounds like a quicker, and simpler thing to try first.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 3

JMatwiejczuk32LYL
Contributor
Contributor
Accepted solution

Hi @WCrihfield, thanks your reply. My code most likely came from your solutions somewhere else in the forum so thanks for all your work on here. 

 

You're right about the active reference pointing to the drawing during the second rule. I checked this by slightly modifying the part or assembly check to throw a message box if the reference wasn't a model or assembly.

 

I also went with your edit suggestion of ThisDoc.ModelDocument and that did at least output a stp file but it was an empty file of 2 KB size.

 

I then tried to get my head around the rule arguments and I think I understand it good enough to know how to give the second rule the exact path of the model it needs to export but I wasn't sure how to modify the rule to use that correctly.

 

Lastly I stumbled on some other threads. one with your solution which brought me closer to what I wanted:

 

https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/run-ilogic-rule-from-an-external-par...

 

and then to:

 

https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/ilogic-open-file-run-rule-and-close-...

 

which gave me my solution so thanks @Anonymous. I used the code in the 4th message on that thread and modified if for my paths and external rule and it works just as I wanted it to. It exports the step file straight from the drawing without even opening the the model so it's quite snappy. To be fair I have no idea how iLogicVb.Automation works but here is the final code for my first rule, the second one is unchanged.

 

doc = ThisDoc.ModelDocument

'name of ilogic rule
Dim oRuleName As String = "FO - STP Rev Date" 

Dim oPartPath As String
If doc.DocumentType = kPartDocumentObject Then
oPartPath = ThisDoc.PathAndFileName(False) & ".ipt"
Else If doc.DocumentType = kAssemblyDocumentObject Then
oPartPath = ThisDoc.PathAndFileName(False) & ".iam"
End If 


doc = ThisApplication.Documents.Open(oPartPath,False)
auto = iLogicVb.Automation
auto.RunExternalRule(doc,"FO - STP Rev Date")
doc.Close(True)