Running drawing rules from rule in assembly

Running drawing rules from rule in assembly

Koekepeerke
Advocate Advocate
1,183 Views
9 Replies
Message 1 of 10

Running drawing rules from rule in assembly

Koekepeerke
Advocate
Advocate

Hey everyone,

 

I'm working on an automation that makes copies of a certain model and positions these in an existing assembly.

after configuration and positioning i'm copying the drawing of the model and i change the model reference to the newly created configuration.

this all works great but now i want to add more customization in the drawing and i started out with a rule for automatic scaling and centering of the views. the problem is however that i cant seem to get the rule to run from the main rule.

 

Sub tekening(modelDWG As String, goot As ComponentOccurrence)

	Dim oPad As String = "C:\\Workspace\NXTdimVault\Documents\Test\Jeremy\Stagemap\Goten"

	Dim oDirSep As Char = System.IO.Path.DirectorySeparatorChar

	Dim gootNaam As String = goot.Definition.Document.FullFileName

	Dim oCopiedDWG As String = oPad & oDirSep & System.IO.Path.GetFileNameWithoutExtension(gootNaam) & ".dwg"

	Dim docum As Inventor.Document


	modelDocDwg = ThisApplication.Documents.Open(modelDWG, False)

	modelDocDwg.SaveAs(oCopiedDWG, False)

	modeldocDwg.close

	gootDWG = ThisApplication.Documents.Open(oCopiedDWG, False)

	Dim oFD As FileDescriptor = gootDWG.ReferencedFileDescriptors(1).DocumentDescriptor.ReferencedFileDescriptor
	oFD.ReplaceReference(gootNaam)
	
	iLogicVb.RunRule(gootDWG.fulldocumentname, "SCALE_&_CENTER")
	
	gootDWG.update()


	ThisApplication.Documents.Open(oCopiedDWG, True)

	For Each docum In ThisApplication.Documents.VisibleDocuments

		If docum.DocumentType = kdrawingdocumentobject Then

			docum.Activate
			
			'HERE THE RULE SHOULD BE RUN------------
			
			'iLogicVb.RunRule(docum.FullDocumentName, "SCALE_&_CENTER")
			'iLogicVb.RunRule(ThisApplication.ActiveDocument, "SCALE_&_CENTER")
			'iLogicVb.RunExternalRule("GOTENPLAN_TEST_V1")
			
			'---------------------------------------
			
			docum.Save

			ThisApplication.CommandManager.ControlDefinitions.Item("VaultCheckinTop").Execute2(False)
			System.Windows.Forms.SendKeys.SendWait(Chr(9) & vbCrLf)

			docum.Close(True)
		End If
	Next
End Sub

 

It either tells me it cant find the document or when i use active document as a reference it will tell me the assembly isnt a drawing but onscreen the drawing is activated and open. 

I'm not sure how to proceed i tried tons of ways of referencing the rule but i cant seem to get it running. any pointers or directions would be greatly appreciated! thanks!

 

Best regards,

Jeremy

0 Likes
Accepted solutions (1)
1,184 Views
9 Replies
Replies (9)
Message 2 of 10

Michael.Navara
Advisor
Advisor

You can call external rule with some arguments

'Main rule
For Each openedDocument As Document In ThisApplication.Documents
	'Run external rule
	Dim ruleArgs As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
	ruleArgs.Add("Document", openedDocument)
	iLogicVb.RunExternalRule("MyExternalRule", ruleArgs)
Next

 

' MyExternalRule stub
Dim doc As Document = RuleArguments("Document")
'Do something with document
Logger.Debug(doc.DisplayName)

 But you can't use some iLogic shortcuts. You need to use full API instead.

0 Likes
Message 3 of 10

Koekepeerke
Advocate
Advocate

First of all thanks for your reply! 

I still can't get it to work note that ik cant reference the document by just typing it in as a string since i generate the document using a numbering scheme as a name. this is how i tried doing it but i already kind of knew it wouldnt work before i tried it. 😅 

 

main rule:

Sub tekening(modelDWG As String, goot As ComponentOccurrence)

	Dim oPad As String = "C:\\Workspace\NXTdimVault\Documents\Test\Jeremy\Stagemap\Goten"

	Dim oDirSep As Char = System.IO.Path.DirectorySeparatorChar

	Dim gootNaam As String = goot.Definition.Document.FullFileName

	Dim oCopiedDWG As String = oPad & oDirSep & System.IO.Path.GetFileNameWithoutExtension(gootNaam) & ".dwg"

	Dim docum As Inventor.Document


	modelDocDwg = ThisApplication.Documents.Open(modelDWG, False)

	modelDocDwg.SaveAs(oCopiedDWG, False)

	modeldocDwg.close

	gootDWG = ThisApplication.Documents.Open(oCopiedDWG, False)

	Dim oFD As FileDescriptor = gootDWG.ReferencedFileDescriptors(1).DocumentDescriptor.ReferencedFileDescriptor
	oFD.ReplaceReference(gootNaam)
	
	gootDWG.update()


	ThisApplication.Documents.Open(oCopiedDWG, True)

	For Each docum In ThisApplication.Documents.VisibleDocuments

		If docum.DocumentType = kdrawingdocumentobject Then

			docum.Activate
			
			

			'----------------------------------------------------------------
			Dim tekDoc As String = docum.FullDocumentName
			
			For Each openendDocument As Document In ThisApplication.Documents
				
				Dim ruleArgs As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
				ruleArgs.Add(tekDoc, openendDocument)
				iLogicVb.RunExternalRule("GOTENPLAN_TEST_V1", ruleArgs)
			Next
			
			'---------------------------------------
			
			docum.Save

			ThisApplication.CommandManager.ControlDefinitions.Item("VaultCheckinTop").Execute2(False)
			System.Windows.Forms.SendKeys.SendWait(Chr(9) & vbCrLf)

			docum.Close(True)
		End If
	Next
End Sub

 

'Sub' rule (external in this case but i have the same rule local in the drawing document):

gootAssy = IO.Path.GetFileName(RuleArguments(tekDoc).ModelDocument.FullFileName)
'gootAssy = IO.Path.GetFileName(thisdrawing.ModelDocument.FullFileName)

gootlengte = Parameter(gootAssy, "goot_lengte")

If gootlengte <= 6250 Then
	ActiveSheet.View("VIEW1").ScaleString = "1:25"
	
ElseIf gootlengte > 6250 And gootlengte <= 7500 Then
	ActiveSheet.View("VIEW1").ScaleString = "1:30"
	
ElseIf gootlengte > 7500 And gootlengte <= 8750 Then
	ActiveSheet.View("VIEW1").ScaleString = "1:35"
	
ElseIf gootlengte > 8750 And gootlengte <= 10000 Then
	ActiveSheet.View("VIEW1").ScaleString = "1:40"
	
ElseIf gootlengte > 10000 Then
	ActiveSheet.View("VIEW1").ScaleString = "1:45"
End If

Dim centerX = ActiveSheet.Width/2 + 5
Dim centerY = ActiveSheet.Height/5 * 3

ActiveSheet.View("VIEW1").SetCenter(centerX, centerY)

Do you have any idea how to fix it with rule arguments?  I am considering just putting the rule as an event trigger in the drawing document but i actually dont want this because then rule would run evertytime i save the document for example.

 

Kind regards,

Jeremy

0 Likes
Message 4 of 10

Michael.Navara
Advisor
Advisor

Please don't just copy + paste my code without thinking and hope it will work.

  • Look how the NameValueMap works. You need to know the key on both sides (Main rule and External rule)
  • Why you iterate documents twice in nested loops

 

 

For Each docum In ThisApplication.Documents.VisibleDocuments
 ...
   For Each openendDocument As Document In ThisApplication.Documents
   ...
   Next
Next

 

 

  • Try to run my code sample and look how it works. Then you can try to change something and look what happens.
0 Likes
Message 5 of 10

Koekepeerke
Advocate
Advocate

You are right i was a bit inpatient at first and i mostly just pastet it in but i did look up a bit of information about rule arguments and i'm not really sure if this is what is need. I don't need to pass a value from one rule to the other i just need to run the rule but somehow either the reference isnt right or the document will not get recognized as 'active' (while it is definitely open onscreen). i did get the external rule to run using the nameValueMap but then i get the error again that the assembly isnt a drawing even though i added the drawing document to the nameValueMap and used the variable in the runexternalrule expression:

 

	For Each docum In ThisApplication.Documents.VisibleDocuments

		If docum.DocumentType = kdrawingdocumentobject Then

			docum.Activate
	
			Dim ruleArgs As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
			ruleArgs.Add("test", docum)
			iLogicVb.RunExternalRule("SCALE_&_CENTER", ruleArgs)

			docum.Save

			ThisApplication.CommandManager.ControlDefinitions.Item("VaultCheckinTop").Execute2(False)
			System.Windows.Forms.SendKeys.SendWait(Chr(9) & vbCrLf)

			docum.Close(True)
		End If
	Next
End Sub 

 I probably just don't understand what i'm doing 😅 ill try a bit longer otherwise i will have to just use the before save event trigger.

0 Likes
Message 6 of 10

Michael.Navara
Advisor
Advisor
Accepted solution

To your original question.

You can run iLogic rule (local/external) in component using this call (I found it right now) In this case you don't need to use ruleArgs. But my experience is to avoid the local rules, because it is very hard to modify them. Try to convert your workflow to external rules.

iLogicVb.Automation.RunRule(doc, "TestLocalRule")
iLogicVb.Automation.RunExternalRule(doc, "ExternalRuleName")

 

To your  current code:

Be careful to close document (docum.Close(True)) in your For Each loop. It modifies the iterated collection ThisApplication.Documents.VisibleDocuments

Message 7 of 10

Koekepeerke
Advocate
Advocate

i don't believe it... 😂 I tried the iLogicVb.runrule call (without the automation in between) a million times with different document references. Normally this works just fine for part and assembly documents. Now i tried the syntax with the automation and boom it suddenly works perfectly. Thanks a lot! I was about to settle for event triggers.

Also thanks for the tips i'll definitely look into that.

 

Kind regards,

Jeremy

0 Likes
Message 8 of 10

sjoerd.van.der.eerden
Contributor
Contributor

hoi, 

ik heb eenzelfde probleem en wil een rule in een tekening laten lopen vanuit mijn hoofd samenstelling. 

het lijkt erop dat dit nu bij jouw is gelukt. ik kom er echter nog niet uit. 

kan je wat tips geven?

0 Likes
Message 9 of 10

Koekepeerke
Advocate
Advocate

hoi @sjoerd.van.der.eerden ,

 

In eerste instantie probeerde ik de rule te lopen met : iLogicVb.RunRule(doc, "Rulenaam")

Zo liet ik altijd andere rules lopen maar schijnbaar werkt dit met tekeningen niet of slecht?

wanneer ik iLogicVb.automation.RunRule(doc, "Rulenaam") probeerde werkte het wel.

Laat maar weten of het is gelukt anders kun je eventueel nog je code sturen.

 

Groet,

Jeremy

 

0 Likes
Message 10 of 10

sjoerd.van.der.eerden
Contributor
Contributor

hoi, 

ja het is gelukt. 

Dim oDoc As Document = ThisDoc.Document
Dim strFolder As String = System.IO.Path.GetDirectoryName(oDoc.FullFileName)
Dim strFileName As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
Dim strDrawname As String = strFolder & "\" & strFileName & ".dwg"
Dim drawdoc As Document = ThisApplication.Documents.Open(strDrawname, True)
drawdoc.activate

Dim iLogicAuto = iLogicVb.Automation
iLogicAuto.RunRule(drawdoc, "rulename")
drawdoc.update

als je deze code laat afspelen opent die je tekening en update die de rule. 

je kan dan natuurlijk ook een regel code schijven op de tekening te openen en dan met event triggers de rule updaten . 

oDWG = ThisDoc.FileName(False) & ".dwg" 'without extension
ThisDoc.Launch(oDWG)

hiermee open je de tekening van het onderdeel of assambly waar je in zit. mits deze dezelfde naam heeft natuurlijk. 

 

ik hoop dat het je helpt. 

0 Likes