Read the instance property in the occurrences of a drawing

Read the instance property in the occurrences of a drawing

kv5053545
Advocate Advocate
565 Views
5 Replies
Message 1 of 6

Read the instance property in the occurrences of a drawing

kv5053545
Advocate
Advocate

I do not know if you can help me I have an assembly with many occurrences when I want to dimension them in an automatic way it has been complicating me a lot, I just found out that there are the instance property I do not know if it is possible to read them from the code so that if it finds a certain letter in the instance property inside the occurrence it makes the dimensions.

 

Dim drawingDoc As DrawingDocument = ThisApplication.ActiveDocument

' Loop through each sheet in the drawing document
For Each sheet As Sheet In drawingDoc.Sheets
    ' Loop through each view on the sheet
    For Each view As DrawingView In Sheet.DrawingViews
        If View.Name = "VIEW1" Then
            ' Check if the view is an assembly view
            ' Get the assembly document for the view
            Dim asmDoc As AssemblyDocument = View.ReferencedDocumentDescriptor.ReferencedDocument

            ' Loop through each assembly component
 For Each comp As ComponentOccurrence In asmDoc.ComponentDefinition.Occurrences
                Dim componentName As String = comp.Name
                Dim Search As String = "Wall"

                If componentName.Contains(Search) Then
 ' Add a dimension to the component if the component name contains "Wall"
Dim ShGeneral = ThisDrawing.Sheets.ItemByName("General:1") 
Dim genDims = ShGeneral.DrawingDimensions.GeneralDimensions
Dim view1 = ShGeneral.DrawingViews.ItemByName("VIEW1") 
 Dim wPl_Wall_FlangeLeft  = view1.GetIntent(comp.Name, "WPl_Wall-FlangeLeft")
 Dim wPl_Wall_FlangeRight = view1.GetIntent(comp.Name, "WPl_Wall-FlangeRight")
  Dim dimX As Double 
 Dim dimY As Double
           '' Add the dimension to the drawing
  Dim linDim1 = genDims.AddLinear("Width_" & componente, view1.SheetPoint(dimX, dimY), wPl_Wall_FlangeLeft, wPl_Wall_FlangeRight)
                        End If

            Next
        End If
    Next
Next

 

0 Likes
Accepted solutions (1)
566 Views
5 Replies
Replies (5)
Message 2 of 6

dalton98
Collaborator
Collaborator
Accepted solution

Yes this is doable. Instance properties are unique for the assembly they are in. They are separate from the typical property sets in an inventor file.

https://modthemachine.typepad.com/my_weblog/2010/02/accessing-iproperties.html

 

Here is an example of how to access them:

Dim oADoc As AssemblyDocument = ThisDoc.Document
For Each comp As ComponentOccurrence In oADoc.ComponentDefinition.Occurrences

	'check if instance property exists
	If comp.OccurrencePropertySetsEnabled = True
		Dim instanceSearch As String = "test"
		For i = 1 To comp.OccurrencePropertySets(1).Count
'			If comp.OccurrencePropertySets(1)(i).Name.Contains("test")
				MessageBox.Show(comp.OccurrencePropertySets(1)(i).Name, comp.OccurrencePropertySets(1)(i).Value)
'			End If
		Next
	End If
Next

 

0 Likes
Message 3 of 6

kv5053545
Advocate
Advocate

Hi, @dalton98  I have seen how to access to the instance property of the occurrences of an assembly but when I try to do the same inside the occurrences that are inside a view in a drawing, I get this error: iProperties: The component named "Wall_A:1" was not found.

 

Dim drawingDoc As DrawingDocument = ThisApplication.ActiveDocument

' Loop through each sheet in the drawing document
For Each sheet As Sheet In drawingDoc.Sheets
    ' Loop through each view on the sheet
    For Each view As DrawingView In Sheet.DrawingViews
        If View.Name = "VIEW1" Then
            ' Check if the view is an assembly view
            ' Get the assembly document for the view
            Dim asmDoc As AssemblyDocument = View.ReferencedDocumentDescriptor.ReferencedDocument

            ' Loop through each assembly component
            For Each comp As ComponentOccurrence In asmDoc.ComponentDefinition.Occurrences
                Dim componente As String = comp.Name 
				
                Dim searchWalls As String = "Wall"

                If componente.Contains(searchWalls) Then
                    ' Add a dimension to the component if the component name contains "Wall"
                    Dim ShGeneral = ThisDrawing.Sheets.ItemByName("General:1") 
                    Dim genDims = ShGeneral.DrawingDimensions.GeneralDimensions
                    Dim view1 = ShGeneral.DrawingViews.ItemByName("VIEW1") 
                    Dim wPl_Wall_FlangeLeft  = view1.GetIntent(componente, "WPl_Wall-FlangeLeft")
                    Dim wPl_Wall_FlangeRight = view1.GetIntent(componente, "WPl_Wall-FlangeRight")
					  
					   
					Dim side = iProperties.InstanceValue(componente, "Side")

                    Dim dimX As Double = 0
                    Dim dimY As Double = 0

If side = A Then
	
	   Dim linDim1 = genDims.AddLinear("SideA" & componente, view1.SheetPoint(dimX, dimY), wPl_Wall_FlangeLeft, wPl_Wall_FlangeRight)
Else If side = B
	
	   Dim linDim1 = genDims.AddLinear("SideB" & componente, view1.SheetPoint(dimX, dimY), wPl_Wall_FlangeLeft, wPl_Wall_FlangeRight)
ElseIf side = C
	
	   Dim linDim1 = genDims.AddLinear("SideC" & componente, view1.SheetPoint(dimX, dimY), wPl_Wall_FlangeLeft, wPl_Wall_FlangeRight)
Else If side = D
	
   Dim linDim1 = genDims.AddLinear("SideD" & componente, view1.SheetPoint(dimX, dimY), wPl_Wall_FlangeLeft, wPl_Wall_FlangeRight)
End If
                  
                End If
            Next
        End If
    Next
Next

		

 

 

0 Likes
Message 4 of 6

A.Acheson
Mentor
Mentor

Hi @kv5053545 

 

Try using the occurrence name to target the correct document

Dim side = iProperties.InstanceValue(componente.Name, "Side")

If that doesn't work then you can try the longer API method that Dalton referred too.

 

'check if instance property exists
	If componente.OccurrencePropertySetsEnabled = True
		Dim instanceSearch As String = "Side"
		For i = 1 To componente.OccurrencePropertySets(1).Count
			If componente.OccurrencePropertySets(1)(i).Name.Contains(instanceSearch)
				MessageBox.Show(componente.OccurrencePropertySets(1)(i).Name, componente.OccurrencePropertySets(1)(i).Value)
			End If
		Next
	End If
If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 5 of 6

kv5053545
Advocate
Advocate

Hi  @A.Acheson @dalton98 ,  Thank you very much, I was able to implement it in my code and it worked very well.
Do you know if it is now possible to create instance property from vb.net to inventor?

0 Likes
Message 6 of 6

dalton98
Collaborator
Collaborator

edit* do it this way

Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim comp As ComponentOccurrence = oADoc.ComponentDefinition.Occurrences(1)

iProperties.InstanceValue(comp.Name, "test") = "hello"

 

@kv5053545 You would add instance values the same way as you find them

Enable property sets for the occurrence > add instance property in the first property set

Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim comp As ComponentOccurrence = oADoc.ComponentDefinition.Occurrences(1)
comp.OccurrencePropertySetsEnabled = True
Dim instProp As Inventor.Property = comp.OccurrencePropertySets(1).Add(12, "name")

 

0 Likes