Running an External Rule for All Sub Parts and Parts under Subassemblies

Running an External Rule for All Sub Parts and Parts under Subassemblies

Anonymous
Not applicable
2,510 Views
9 Replies
Message 1 of 10

Running an External Rule for All Sub Parts and Parts under Subassemblies

Anonymous
Not applicable

Hi,

 

I want to run an external rule for all part from top to bottom. I have found a code in the forum but it is working only for top assembly.

 

Can you help me about this ?

 

Sub Main()
    Dim oDoc As Document
    oDoc = ThisApplication.ActiveDocument
    
    Call Adding(oDoc.ComponentDefinition.Occurrences, 0)

End Sub
Sub Adding(Occurrences As ComponentOccurrences, _
                             Level As Integer)
    Dim oOcc As ComponentOccurrence

	
iLogicVb.RunExternalRule("deneme2")

For Each oOcc In Occurrences
     'If it is an Assembly go to next Level
     If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
      	Call Adding(oOcc.SubOccurrences, Level + 1) 
     Else
     End If
Next
End Sub


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

JelteDeJong
Mentor
Mentor

At first glance i don't see strange things in your code, it should find all parts. But i wonder how your iLogic code "deneme2" knows which occurrence (or part) it should use....

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 3 of 10

Anonymous
Not applicable

Hi,

 

deneme2 code is something like this ( a lot of try catch): 

 

	Dim PropertyName1 As String = "CHardness"
	Dim propertyValue As String = ""

	customPropertySet = ThisDoc.Document.PropertySets.Item("Inventor User Defined Properties")
	Try
	      prop = customPropertySet.Item(PropertyName1)
	Catch
	      customPropertySet.Add("", PropertyName1)
		  iProperties.Value("Custom", PropertyName1) = propertyValue
	End Try



 

0 Likes
Message 4 of 10

Anonymous
Not applicable

This works for me for user parameters but custom parameters won't work. I think other code should be modified.

 

'Define the open document
Dim oDoc As Document
oDoc = ThisDoc.Document

'Dim oDoc As AssemblyDocument
'oDoc = ThisApplication.ActiveDocument

If Not ThisApplication.ActiveDocument.DocumentType = kAssemblyDocumentObject Then 
	Return
End If
'Look at all of the files referenced in the open document
Dim docFile As Document
For Each docFile In oDoc.AllReferencedDocuments
	
ThisApplication.Documents.Open(docFile.FullFileName, True)


iLogicVb.RunExternalRule("deneme2")

docFile.Save
docFile.Close
	
Next

iLogicVb.UpdateWhenDone = True

 

deneme2 rule:

customPropertySet = ThisDoc.Document.PropertySets.Item("Inventor User Defined Properties")

			Dim PropertyName1 As String = "CHardness"
			Dim propertyValue As String = ""

	Try
	      prop = customPropertySet.Item(PropertyName1)
	Catch
			
	      customPropertySet.Add("", PropertyName1)
		  iProperties.Value("Custom", PropertyName1) = propertyValue
	End Try
	
	
'--------------------------------------------------------
			oMyParameter=ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters
				'Setting an userParameter as text parameter
				Try
					oMyParameter("Mass1") = Parameter("Mass1")
					Catch
				oParameter = oMyParameter.AddByValue("Mass1", "", UnitsTypeEnum.kTextUnits)
			End Try
				oParameter = oMyParameter.AddByValue("Material1", "", UnitsTypeEnum.kTextUnits)
				oParameter = oMyParameter.AddByValue("SDef1", "", UnitsTypeEnum.kTextUnits)
				oParameter = oMyParameter.AddByValue("SDef2", "", UnitsTypeEnum.kTextUnits)
				oParameter = oMyParameter.AddByValue("SDef3", "", UnitsTypeEnum.kTextUnits)
				oParameter = oMyParameter.AddByValue("SDef4", "", UnitsTypeEnum.kTextUnits)
				

	
	
Parameter.UpdateAfterChange = True
iLogicVb.UpdateWhenDone = True



iLogicVb.RunExternalRule("StokTanım")
iLogicVb.RunExternalRule("Alan")



iProperties.Value("Custom", "StockNo_2") = iProperties.Value("Project", "Authority")
iProperties.Value("Custom", "StockNo_3") = iProperties.Value("Summary", "Category")
iProperties.Value("Custom", "Quantity") = iProperties.Value("Project", "WEB LINK")
iProperties.Value("Status", "Checked By") = iProperties.Value("Summary", "Manager") 
iProperties.Value("Summary", "Manager") = "Mehmet Evrenosoğlu"


	Try
	iProperties.Value("Custom", "Title_2") = iProperties.Value("Summary", "Company")
	Catch
	End Try
	Try
	iProperties.Value("Custom", "Subject_2") = iProperties.Value("Custom", "ASSM.NAME")
	Catch
	End Try

Parameter.UpdateAfterChange = True
iLogicVb.UpdateWhenDone = True

 Kindly ask for your help.

0 Likes
Message 5 of 10

J-Camper
Advisor
Advisor
Accepted solution

I played around a bit with your code, and I think this will work:

Main Code:

 

Sub Main()
    'Make sure rule is running in Assembly Document.  Not needed if rule is stored locally in assembly file.
	If ThisDoc.Document.DocumentType <> kAssemblyDocumentObject
		Exit Sub
	End If
	
	Dim oDoc As AssemblyDocument
    oDoc = ThisDoc.Document
   	
	Dim oRefDoc As Document
	
	'Look at all documents referenced in this assembly
	For Each oRefDoc In oDoc.AllReferencedDocuments
		'Only looking for part documents that are modifiable
 		If oRefDoc.DocumentType = kPartDocumentObject And oRefDoc.IsModifiable = "True" 
			Call RunPartDocuments(oRefDoc)
   		End If
	Next	
    
End Sub

Sub RunPartDocuments(PassDoc As PartDocument)

Dim refPartDoc As PartDocument = ThisApplication.Documents.Open(PassDoc.FullFileName, True)

iLogicVb.RunExternalRule("deneme2")

refPartDoc.Save2()
refPartDoc.Close

End Sub

 

External Rule Code:

Sub Main
'Make sure document is a Part Document
If ThisApplication.ActiveDocument.DocumentType <> kPartDocumentObject
	MessageBox.Show("This Document is not a part document, but this rule is set up only to work with Part Documents.", "Canceling Rule")
	Exit Sub
End If

'[
'Access Custom iProperties, and create missing Properties
customPropertySet = ThisApplication.ActiveDocument.PropertySets.Item("Inventor User Defined Properties")

Dim PropertyName As String() 
Dim propertyValue As String() 

PropertyName= {"CHardness", "StockNo_2", "StockNo_3", "Quantity", "Title_2", "Subject_2", "ASSM.NAME"}
propertyValue= {"testing", "Authority", "Category",        "1",    "Title",   "Subject",   "Parent"}			

Dim i As Integer = 0
While i < PropertyName.Count

	Try
	      prop = customPropertySet.Item(PropertyName(i))
	Catch
	      customPropertySet.Add("", PropertyName(i))
		  customPropertySet.Item(PropertyName(i)).Value = propertyValue(i)
	End Try
	i = i+1
	
End While
'] Access Custom iProperties, and create missing Properties

'[
'Access local UserParameters , and create missing Parameters
oMyParameter = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters

Dim Parameters As String() 

Parameters = {"Mass1", "Material1", "SDef1", "SDef2", "SDef3", "SDef4"}


Dim j As Integer = 0
While j < Parameters.Count

	Try
	  	Params = oMyParameter(Parameters(j)) ' Asking if this  Parameter exists
	Catch
		Params = oMyParameter.AddByValue(Parameters(j), "", UnitsTypeEnum.kTextUnits) ' If not create this parameter
	End Try
	
	j = j+1

End While
'] Access local UserParameters , and create missing Parameters

'Update mid-rule
Parameter.UpdateAfterChange = True
iLogicVb.UpdateWhenDone = True

'Run other External Rules
'iLogicVb.RunExternalRule("StokTanım")
'iLogicVb.RunExternalRule("Alan")

'Assuming the previous 2 rules will modify Property values, and setting thsoe values:
customPropertySet.Item("StockNo_2").Value = ThisApplication.ActiveDocument.PropertySets.Item("Design Tracking Properties").Item("Authority").Value
customPropertySet.Item("StockNo_3").Value = ThisApplication.ActiveDocument.PropertySets.Item("Inventor Document Summary Information").Item("Category").Value
customPropertySet.Item("Quantity").Value = ThisApplication.ActiveDocument.PropertySets.Item("Design Tracking Properties").Item("Catalog Web Link").Value
ThisApplication.ActiveDocument.PropertySets.Item("Design Tracking Properties").Item("Checked By").Value = "Mehmet Evrenosoğlu"
ThisApplication.ActiveDocument.PropertySets.Item("Inventor Document Summary Information").Item("Manager").Value = "Mehmet Evrenosoğlu"
customPropertySet.Item("Title_2").Value = ThisApplication.ActiveDocument.PropertySets.Item("Inventor Document Summary Information").Item("Company").Value
customPropertySet.Item("Subject_2").Value = customPropertySet.Item("ASSM.NAME").Value

'Update When Done
Parameter.UpdateAfterChange = True
iLogicVb.UpdateWhenDone = True

End Sub

 

I don't know what the other two External rules within deneme2 do so they are commented out in the above code, but I assumed they updated iProperty Values set after them.

 

 

Also for your information I have attached an excel sheet with PropertySet Names and Their corresponding Properties.

0 Likes
Message 6 of 10

Anonymous
Not applicable

You are absoultly right. Thank you so much for your hard work !

0 Likes
Message 7 of 10

Anonymous
Not applicable

It is skipping assembly documents, is it possible to add assembly documents too ?

0 Likes
Message 8 of 10

J-Camper
Advisor
Advisor

It should be including all the parts within those assemblies with "AllReferencedDocuments".  but if you want to run the containing assemblies too, you can add an "Else If" statement where you are sifting through "AllReferencedDocuments":

'Look at all documents referenced in this assembly
	For Each oRefDoc In oDoc.AllReferencedDocuments
		'Only looking for part documents that are modifiable
 		If oRefDoc.DocumentType = kPartDocumentObject And oRefDoc.IsModifiable = "True" 
			Call RunPartDocuments(oRefDoc)
   		Else If oRefDoc.DocumentType = kAssemblyDocumentObject And oRefDoc.IsModifiable = "True" 
                        'Do Assembly stuff
                End If
	Next	

If you plan to run the assemblies through the same External rule, I think the iProperties and User Parameters are mapped the same way so it should work.  You would need to modify the Document type check in the External rule:

'Make sure document is a Part or Assembly Document
If ThisApplication.ActiveDocument.DocumentType = kPartDocumentObject Or _
   ThisApplication.ActiveDocument.DocumentType = kAssemblyDocumentObject
	'Continue
Else	
	MessageBox.Show("This Document is not a Part or Assembly document, but this rule is set up only to work with Part Documents.", "Canceling Rule")
	Exit Sub
End If

Although I still don't know what those other two External rules are doing. If you try to do more document type specific things, like looking at surfacebodies in a part or occurrences in an assembly, you definitely need to separate the External rule or you will get errors.

 

Or you can run a separate External rule for the Assembly documents, so that you can look at assembly document specific Objects and not modify your Part specific External rule.

0 Likes
Message 9 of 10

Anonymous
Not applicable

I reshape my rules, thanks to your contributions.

 

Here is my main rule: 

RRFA=MessageBox.Show("Bu komutu çalıştırmadan önce tüm CHECKOUT'ları almış olmanız gerekmektedir. Eğer checkout almadıysanız lütfen işlemi iptal ederek checkout alın. Referans olarak çekilmiş .stp parçalar montajda var ise program hata verecektir. Devam edilsin mi?", "ÇOK ÖNEMLİ UYARI",MessageBoxButtons.YesNo,MessageBoxIcon.Warning)


If RRFA = vbYes Then
	
	RRFA2= MessageBox.Show("Tüm montaj için parametreleri eklemeye başlamak üzeresiniz. İşlem süresince Inventor'e müdahale etmeyiniz. Checkout'u sizde olmayan parçalar açıldıktan sonra kapanmayacaktır. Montajın büyüklüğüne göre işlem süresi çok uzun olabilir. Devam etmek istediğinize emin misiniz ?", "Köprüden Önceki Son Çıkış",MessageBoxButtons.YesNo,MessageBoxIcon.Warning)

If RRFA2 = vbYes Then
		
Dim oDoc As Document
oDoc = ThisDoc.Document

'Dim oDoc As AssemblyDocument
'oDoc = ThisApplication.ActiveDocument

If Not ThisApplication.ActiveDocument.DocumentType = kAssemblyDocumentObject Then 
	Return
End If
'Look at all of the files referenced in the open document
Dim docFile As Document
For Each docFile In oDoc.AllReferencedDocuments
	
ThisApplication.Documents.Open(docFile.FullFileName, True)

'iLogicVb.RunExternalRule("PCustomUser")

iLogicVb.RunExternalRule("RunRuleForAll2")

docFile.Save
docFile.Close
	
Next

	MessageBox.Show("İşlem tamamlandı.", "Mutlu Son")


	Else
	MessageBox.Show("İşlem iptal edildi.", "ÇOK ÖNEMLİ UYARI")
	End If
Else
	MessageBox.Show("İşlem iptal edildi.", "ÇOK ÖNEMLİ UYARI")

End If
iLogicVb.UpdateWhenDone = True

 Here is second rule RunRuleForAll2

			'Msj= MessageBox.Show("Bu makroyu halihazırda mevcut olan TR-EN montaj ve partlar için çalıştırırsanız olmayan parametreler eklenir ve eski antetten yeni antete aktarım yapılır. Önceden tanımlanmış olanlar olduğu gibi kalır. Devam etmek istediğinize emin misiniz ?", "Son Karar",MessageBoxButtons.YesNo)

			'If Msj = vbYes Then
customPropertySet = ThisApplication.ActiveDocument.PropertySets.Item("Inventor User Defined Properties")

Dim PropertyName As String() 
Dim propertyValue As String

PropertyName = {"CHardness", "SHardness", "Hblack", "HGreen", "MRed", "MBlack", "SBlue","Quantity", "StockNo_2", "StockNo_3", "StockNo_4", "StockDef1", "StockDef2", "StockDef3", "StockDef4", "Quantity", "Title_2", "Subject_2"}
propertyValue= ""			

Dim a As Integer = 0
While a < PropertyName.Count

	Try
	      prop = customPropertySet.Item(PropertyName(a))
	Catch
	      customPropertySet.Add("", PropertyName(a))
		  customPropertySet.Item(PropertyName(a)).Value = propertyValue
	End Try
	a = a+1
	
End While
			'-----------------------------
			Dim PropertyName18 As String = "ToleranceClass"
			Dim propertyValue1 As String = "ISO 2768 CL"

			Try
			prop = customPropertySet.Item(PropertyName18)
			Catch
			customPropertySet.Add("", PropertyName18)
			iProperties.Value("Custom", PropertyName18) = propertyValue1
			End Try
			'-----------------------------------------------------------------------------------------
			
			'Name=ThisApplication.ActiveDocument.FullDocumentName
		'	Dim name1 As String
		'name1=ThisDoc.FileName(True) 'without extension
		customPropertySet1 = ThisApplication.ActiveDocument.PropertySets.Item("Design Tracking Properties")
		customPropertySet2 = ThisApplication.ActiveDocument.PropertySets.Item("Document Summary Information")
			
			customPropertySet.Item("StockNo_2").Value = customPropertySet1.Item("Authority").Value

			customPropertySet.Item("StockNo_3").Value = customPropertySet2.Item("Category").Value

			customPropertySet.Item("Quantity").Value = customPropertySet1.Item("Catalog Web Link").Value 


'KONTROL EDEN TANIMLAMA - TASARIMCININ APPLICATION OPTION'DAKI USER NAME'İNE GÖRE BELİRLEMEKTEDİR.

ThisApplication.ActiveDocument.PropertySets.Item("Design Tracking Properties").Item("Designer").Value=ThisApplication.GeneralOptions.UserName
Dim Designer As String
Designer=ThisApplication.ActiveDocument.PropertySets.Item("Design Tracking Properties").Item("Designer").Value
Dim Designers()={"Okan Murat", "Tayfun Ayyıldız", "Ömür Yıldırım", "Barış Kök", "Engin Oylukan", "Gençay Genç", "Hakan Gümüş", "Recep Başaran"}', "Mustafa Güner"}
Dim MaxCount As Integer
Dim Counter As Integer
i = 0
MaxCount = 7
For Counter=i To MaxCount
If Designer = Designers(i) Then
	ThisApplication.ActiveDocument.PropertySets.Item("Design Tracking Properties").Item("Checked By").Value = "Recep Gümrük"
	Else
		ThisApplication.ActiveDocument.PropertySets.Item("Design Tracking Properties").Item("Checked By").Value = "Zülkani Durmuş"
End If
i = i + 1
Next
'KONTROL EDEN TANIMLAMA SON

ThisApplication.ActiveDocument.PropertySets.Item("Inventor Document Summary Information").Item("Manager").Value = "Mehmet Evrenosoğlu"

			'Mevcutsa aktarım:	
			'MessageBox.Show("Message", "Title")

			Try
			customPropertySet.Item("Title_2").Value = customPropertySet2.Item("Company").Value
			Catch
			End Try

			Try
			customPropertySet.Item("Subject_2").Value = customPropertySet.Item("ASSM.NAME").Value
			Catch
			End Try
'---------------------------------------------------------------------------------------------
			Dim ParamValue2 As Double = 0
			Dim ParamName2 = "Alan"

			Dim oDoc As Document = ThisApplication.ActiveDocument
			Dim oCD As ComponentDefinition = oDoc.ComponentDefinition
			Dim oParams As Parameters = oCD.Parameters
			Dim oUserParams As UserParameters = oParams.UserParameters

			Try
			oParams(ParamName2).Value = ParamValue2
			Catch
			Dim newparam As UserParameter= oUserParams.AddByExpression(ParamName2, ParamValue2, "m^2")
			'oParams(ParamName2).Expression = iProperties.Area
			oParams(ParamName2).Value= iProperties.Area

			newparam.ExposedAsProperty=True
			'oParams.IsKey=True
			'oUserParams.AddByValue(ParamName,6,11270)
			End Try

			'--------------------------------------------------------
			oMyParameter = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters

Dim Parameters As String() 

Parameters = {"Mass", "Material", "SDef1", "SDef2", "SDef3", "SDef4"}
ParameterVal = {"", "", "", "", "", ""}

Dim j As Integer = 0
While j < Parameters.Count

	Try
	  	Params = oMyParameter(Parameters(j)) ' Asking if this  Parameter exists
	Catch
		Params = oMyParameter.AddByValue(Parameters(j), "", UnitsTypeEnum.kTextUnits) ' If not create this parameter
		'Params.Iskey=True 
	End Try
	
	j = j+1

End While


			Parameter.UpdateAfterChange = True
			iLogicVb.UpdateWhenDone = True
ThisDoc.Save

iLogicVb.RunExternalRule("StokTanım")

			'MessageBox.Show("Olmayan parametreler eklendi ve eski antetten yeni antete aktarım yapıldı. Her şey sen daha mutlu ol diye el birliği ile çalışıyor farkında mısın ?" & vbNewLine  & vbNewLine  & "STOK TANIMLAMALARINI DEĞİŞTİRMEYİ UNUTMA !!!", "Yaşıyorsun Şu Hayatı", MessageBoxButtons.OK)

			'MessageBox.Show("Stok tanımlamala", "Yaşıyorsun Şu Hayatı", MessageBoxButtons.OK)
			Parameter.UpdateAfterChange = True
			iLogicVb.UpdateWhenDone = True
ThisApplication.ActiveDocument.Save

			'-----------------------------
			'Else
			'MessageBox.Show("İşlem iptal edildi.", "Peki...",MessageBoxButtons.OK)
			'End If

 Finally last Rule StokTanım: 

oMyParameter = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters
oCustomPropertySet = ThisApplication.ActiveDocument.PropertySets.Item("Inventor User Defined Properties")
	MultiValue.SetList("SDef1", "",  "Stok Numarası Stock No", "Boyalı SN Painted SN", "Kaynaklı SN Welded SN", "Talaşlı SN Machining SN",
 "Kaplama SN Coating SN","Montaj SN Assembly SN")
MultiValue.SetList("SDef2", "",  "Stok Numarası Stock No", "Boyalı SN Painted SN", "Kaynaklı SN Welded SN", "Talaşlı SN Machining SN",
 "Kaplama SN Coating SN","Montaj SN Assembly SN")
MultiValue.SetList("SDef3", "", "Stok Numarası Stock No", "Boyalı SN Painted SN", "Kaynaklı SN Welded SN", "Talaşlı SN Machining SN",
 "Kaplama SN Coating SN","Montaj SN Assembly SN")
MultiValue.SetList("SDef4", "",  "Stok Numarası Stock No", "Boyalı SN Painted SN", "Kaynaklı SN Welded SN", "Talaşlı SN Machining SN",
 "Kaplama SN Coating SN","Montaj SN Assembly SN")
 
oCustomPropertySet.Item("StockDef1").Value = oMyParameter("SDef1")
oCustomPropertySet.Item("StockDef2").Value = oMyParameter("SDef1")
oCustomPropertySet.Item("StockDef3").Value = oMyParameter("SDef2")
oCustomPropertySet.Item("StockDef4").Value = oMyParameter("SDef3")

'set custom property value
'iProperties.Value("Custom", "StockDef3") = Parameter.Value("SDef3")
'iProperties.Value("Custom", "StockDef4") =Parameter.Value("SDef4")

'iLogicVb.RunExternalRule("Alan")
Parameter("Alan") = iProperties.Area

Parameter("Material") = iProperties.Material

Parameter("Mass")= iProperties.Mass	

iLogicVb.UpdateWhenDone = True
iLogicVb.UpdateWhenDone = True

As you can guess i am getting errors. 

 

0 Likes
Message 10 of 10

Michele_DeLucaSW7PJ
Contributor
Contributor

i get an errore on line 23: 

ThisApplication.Documents.Open(docFile.FullFileName, True)

Michele_DeLucaSW7PJ_0-1720604124218.png

 

 

0 Likes