Hi all,
Can anyone help me with some example iLogic code to change an iproperty value of a selected part or parts in an assembly.
Thanks in advance
Stryder
Invetor 2016
windows 7
Solved! Go to Solution.
Solved by Owner2229. Go to Solution.
Hi, you mean like this? As it is it's without the selection, so it simply changes all the parts. I believe you can set up the selection by yourself, but let me know if not.
Dim oDoc As Document = ThisApplication.ActiveDocument Dim aDoc As DocumentsEnumerator = oDoc.AllReferencedDocuments Dim iDoc As Document For Each iDoc In aDoc 'Here we set iProperties in each of the parts in assembly Dim sTS As String = iDoc.FullFileName Dim FNamePos As Long = InStrRev(sTS, "\", - 1) Dim docFName As String = Mid(sTS, FNamePos + 1, Len(sTS) - FNamePos) iProperties.Value(docFName, "Custom", "PropertyName1") = "some text" iProperties.Value(docFName, "Custom", "PropertyName2") = "more text" Next 'Here we set the assembly's iProperties iProperties.Value("Custom", "PropertyName1") = "even more text" iProperties.Value("Custom", "PropertyName2") = "and more"
I have writen the same code for all parts in an assembly before. I am just having a little trouble incorporating the selectset to it. I would like to be able to add a 'mark' iproperty to a selected part regardless if that part exists in the assembly or a sub assembly.
Hi, try this and let me know if it does what you want.
It will ask you to select part(s). Select as mayn as you want and end the selection with "Esc".
Sub Main() Dim oDoc As Document oDoc = ThisApplication.ActiveDocument Dim comps As ObjectCollection Dim comp As ComponentOccurrence oDoc.SelectSet.Clear comps = ThisApplication.TransientObjects.CreateObjectCollection While True comp = ThisApplication.CommandManager.Pick( SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select Part(s)") 'If nothing gets selected then we're done If IsNothing(comp) Then Exit While comps.Add(comp) End While 'If there are selected components we can do something, otherwise we're done If comps.count = 0 Then Exit Sub Dim aDoc As DocumentsEnumerator aDoc = oDoc.AllReferencedDocuments Dim iDoc As Document Dim cName As String Dim cTS As String Dim sTS As String Dim FNP As Long Dim cFNP As Long Dim docFN As String For Each iDoc In aDoc sTS = iDoc.FullFileName FNP = InStrRev(sTS, "\", - 1) docFN = Mid(sTS, FNP + 1, Len(sTS) - FNP) For Each comp In comps cTS = comp.Name cFNP = InStrRev(cTS, ":", - 1) cName = Left(cTS, cFNP - 1) If cName = Left(docFN, Len(docFN)-4) Then 'Set iProperty in each of the selected parts in assembly iProperties.Value(docFN, "Custom", "Mark") = "this part is now marked" End If Next Next oDoc.SelectSet.Clear End Sub
This will only mark a part in the assembly you are in, not in a sub assembly. Also is there anyway you can do this with select set instead? (so u could set the property of sub assemblies based on your component priority filter)
Here is version that uses SelectSet.
It WORKS on parts in subassemblies. I tested it.
Sub Main() Dim oDoc As Document oDoc = ThisApplication.ActiveDocument Dim comps As SelectSet Dim comp As ComponentOccurrence comps = oDoc.SelectSet 'If there are selected components we can do something, otherwise we're done If comps.count = 0 Then Exit Sub Dim aDoc As DocumentsEnumerator aDoc = oDoc.AllReferencedDocuments Dim iDoc As Document Dim cName As String Dim cTS As String Dim sTS As String Dim FNP As Long Dim cFNP As Long Dim docFN As String For Each iDoc In aDoc sTS = iDoc.FullFileName FNP = InStrRev(sTS, "\", - 1) docFN = Mid(sTS, FNP + 1, Len(sTS) - FNP) For Each comp In comps cTS = comp.Name cFNP = InStrRev(cTS, ":", - 1) cName = Left(cTS, cFNP - 1) If cName = Left(docFN, Len(docFN)-4) Then 'Set iProperty in each of the selected parts in assembly iProperties.Value(docFN, "Custom", "Mark") = "this part is now marked" End If Next Next End Sub
Cheers for that, works perfectly. Sorry the first code does work on parts in sub assemblies, I tested it in a different model and it was fine. There must have been something wrong in the first model I tested it on. Thanks for all your help it is much appreciated.
Like the code much, but one question. I'm changing 2 custom properties and values in code. Could it be possible to run rule from eg. form and and select from list value that will be replaced with.
Thank you
Surely it will be possible, but first I'd like to clarify some things:
1a) Do you want to change iProperty value based on document's name?
1b) Do you want to change iProperty value globally (same value for all documents)?
2) Do you want to change the iPro values for parts or only sub-assemblies?
3) Do you want to pre-populate the values or let the user write in custom values?
E.g.:
3a) "Select the value for iProA: Value1 "
Value2 "
3b) "Write in the value for iProA: _____"
4) Do you want to create the iPro if it doesn't exist?
5) Do you want to write the iPro in the top-assy as well?
The example below is using:
1) B - globally
2) Both
3) Write in
4) Yes
5) No
Sub Main() Dim Val1 As String = InputBox("Write in the value for iProA:", "iProA") Dim oDoc As Document = ThisApplication.ActiveDocument For Each iDoc In oDoc.AllReferencedDocuments Dim sFN As String = iDoc.FullFileName Dim FNP As Integer = InStrRev(sFN, "\", - 1) Dim docFN As String = Mid(sFN, FNP + 1, Len(sFN) - FNP) iProperties.Value(docFN, "Custom", "iProA") = Val1 Next End Sub
Hi Mike,
first thanks for so fast answer.
I try this code and works good. What I do have here is this: We have same product but different color and material.
I need to change custom material iprop and material description custom iprop. Since is in assembly few different materials I can't change everything at once. So I would like to change Material # 123456 with 23456, and Material Description aaaa into bbbb which correspond to those material numbers.
Thank you
Alright, we can try something like this:
Imports System AddReference "System.Drawing.dll" Imports System.Windows.Forms Sub Main() iPro_Mat = "Material-iPro-Name" iPro_Des = "Description-iPro-Name" Dim Mat_List() As String = {"123456", "234567"} Dim Desc_List() As String = {"Desc1", "Desc2"} CreateForm("MyReplacingForm") AddLB(5, 5, "Material to replace:", 160) AddLB(5, 32, "Replacing material:", 160) AddCB(170, 5, Mat_List, 150) AddCB(170, 32, Mat_List, 150) AddLB(5, 75, "Description to replace:", 160) AddLB(5, 102, "Replacing description:", 160) AddCB(170, 75, Desc_List, 150) AddCB(170, 102, Desc_List, 150) Dim oBT As Button = AddBT(0, 0, "REPLACE") Dim PosX As Integer = MF.Width - (oBT.Width * 1.3) - 80 Dim PosY As Integer = MF.Height - (oBT.Height * 3) oBT.Location = New Drawing.Point(PosX, PosY) AddHandler oBT.Click, AddressOf Me.Replace MF.ShowDialog() End Sub Private Sub Replace() Dim Val1 As String = CB(0).Text Dim Val2 As String = CB(1).Text Dim Val3 As String = CB(2).Text Dim Val4 As String = CB(3).Text If Val1 = vbNullString And Val3 = vbNullString Then Exit Sub Dim oDoc As Document = ThisApplication.ActiveDocument For Each iDoc As Document In oDoc.AllReferencedDocuments If Val1 <> vbNullString Then Dim ValX As String = iPropertyUser(iDoc, iPro_Mat).Expression If ValX = Val1 Then iPropertyUser(iDoc, iPro_Mat).Expression = Val2 End If If Val3 <> vbNullString Then Dim ValX As String = iPropertyUser(iDoc, iPro_Des).Expression If ValX = Val3 Then iPropertyUser(iDoc, iPro_Des).Expression = Val4 End If
Next End Sub Private Function iPropertyUser(oDoc As Inventor.Document, oProp As String) As Inventor.Property Dim oPropsets As PropertySets = oDoc.PropertySets Dim oPropSet As PropertySet = oPropsets.Item("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}") Try iPropertyUser = oPropSet.Item(oProp) Catch oPropSet.Add("", oProp) iPropertyUser = oPropSet.Item(oProp) End Try oPropsets = Nothing oPropSet = Nothing End Function Private iPro_Mat As String Private iPro_Des As String Private MF As Form Private BT(0) As Button Private CB(0) As ComboBox Private LB(0) As Label Private Sub CreateForm(Optional Name As String = vbNullString) MF = New Form MF.Text = Name MF.AutoScaleMode = AutoScaleMode.None MF.Size = New Drawing.Size(350, 210) 'Width, Heigth MF.MinimumSize = MF.Size MF.MaximumSize = MF.Size MF.Font = New Drawing.Font(MF.Font.FontFamily, 10) MF.MaximizeBox = False MF.MinimizeBox = False MF.ShowIcon = False MF.SizeGripStyle = SizeGripStyle.Hide MF.StartPosition = FormStartPosition.CenterScreen AddExitButton() End Sub Private Function AddLB(PosX As Integer, PosY As Integer, Optional Caption As String = vbNullString, Optional Width As Integer = 100) As Label Dim LC As Integer = LB.Length - 1 If Not LB(LC) Is Nothing Then LC = LC + 1 ReDim Preserve LB(LC) End If LB(LC) = New Label LB(LC).Name = "L" & LC LB(LC).Location = New Drawing.Point(PosX, PosY) LB(LC).Text = Caption LB(LC).Width = Width MF.Controls.Add(LB(LC)) Return LB(LC) End Function Private Function AddCB(PosX As Integer, PosY As Integer, Values() As String, Optional Width As Integer = 100) As ComboBox Dim LC As Integer = CB.Length - 1 If Not CB(LC) Is Nothing Then LC = LC + 1 ReDim Preserve CB(LC) End If CB(LC) = New ComboBox CB(LC).Location = New Drawing.Point(PosX, PosY) CB(LC).Name = "CB" & LC CB(LC).Width = Width For Each Value As String In Values CB(LC).Items.Add(Value) Next MF.Controls.Add(CB(LC)) Return CB(LC) End Function Private Function AddBT(PosX As Integer, PosY As Integer, Caption As String, Optional Width As Integer = 80) As Button Dim LC As Integer = BT.Length - 1 If Not BT(LC) Is Nothing Then LC = LC + 1 ReDim Preserve BT(LC) End If BT(LC) = New Button BT(LC).Name = "BT" & LC BT(LC).Location = New Drawing.Point(PosX, PosY) BT(LC).Text = Caption BT(LC).Width = Width MF.Controls.Add(BT(LC)) Return BT(LC) End Function Private Sub AddExitButton() Dim oBT As Button = AddBT(0, 0, "OK") Dim PosX As Integer = MF.Width - (oBT.Width * 1.3) Dim PosY As Integer = MF.Height - (oBT.Height * 3) oBT.Location = New Drawing.Point(PosX, PosY) AddHandler oBT.Click, AddressOf Me.ExitButtonClick End Sub Private Sub ExitButtonClick() MF.Close() End Sub