Giving an attribute (name) to the face with largest area using iLogic

Giving an attribute (name) to the face with largest area using iLogic

frahaman7SKBG
Advocate Advocate
1,072 Views
9 Replies
Message 1 of 10

Giving an attribute (name) to the face with largest area using iLogic

frahaman7SKBG
Advocate
Advocate

Hi guys, 

I'm working on complex design automation. As I am working through the problem, I realize I need to detect the largest face of my part and give it a name (attribute). Once the name is given to that face, I can use the 'Angle' measure snippet to measure the angle between that face and one of the origin planes. And then continue with the rest of the code. 

 

If anyone here knows how to make this or points to a resource where I can find some guidance, it will be greatly appreciated. 

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

JelteDeJong
Mentor
Mentor

is this what you are looking for:

		Dim doc As PartDocument = ThisDoc.Document
		Dim bodies = doc.ComponentDefinition.SurfaceBodies

		Dim biggestFaceArea As Double = Double.MinValue
		Dim biggestFace As Face = Nothing
		For Each body As SurfaceBody In bodies
			For Each face As Face In body.Faces
				Dim area = face.Evaluator.Area
				If (biggestFaceArea < area) Then
					biggestFace = face
					biggestFaceArea = area
				End If
			Next
		Next

		Dim attSet = biggestFace.AttributeSets.Add("hjalte.nl")
		Dim att = attSet.Add("biggest", ValueTypeEnum.kDoubleType, biggestFaceArea)

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

JelteDeJong
Mentor
Mentor

Just because it can be done. It's also possible without loops 😁

Dim doc As PartDocument = ThisDoc.Document

Dim biggestFace = doc.ComponentDefinition.SurfaceBodies.
	Cast(Of SurfaceBody).
	SelectMany(Of Face)(Function(f) f.Faces.Cast(Of Face)).
	Aggregate(Function(face, nextFace) If(face.Evaluator.Area > nextFace.Evaluator.Area, face, nextFace))


Dim attSet = biggestFace.AttributeSets.Add("hjalte.nl")
Dim att = attSet.Add("biggest", ValueTypeEnum.kDoubleType, biggestFace.Evaluator.Area)

 

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 4 of 10

frahaman7SKBG
Advocate
Advocate

Hello @JelteDeJong 

Thank you for taking the time and helping me with this problem. 

 

I have tried running your code but for some reason, I'm getting an error on this line: 

 

Dim attSet = biggestFace.AttributeSets.Add("hjalte.nl")

 

I have also tried following the method described on this blog (link at the btm), and it looks like I'm getting similar errors when trying to add an Attribute to the part using the code from it. It says the parameter is incorrect. 

 

frahaman7SKBG_0-1662230910499.png

 

 

Blog: https://modthemachine.typepad.com/my_weblog/2009/07/introduction-to-attributes.html

 

0 Likes
Message 5 of 10

JelteDeJong
Mentor
Mentor

The code will only work if the attribute set is not created jet. That means it probably only runs once. Did you check if the attribute was created? (A good tool for that is the Nifty Attributes addin from @BrianEkins )

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 6 of 10

frahaman7SKBG
Advocate
Advocate

You are correct, it does look like the attribute was created. I was able to check using the code below. 

Why does the attribute not appear under the Entities list in the iLogic browser? When I tried calling the attribute for my next calculation, it said the Entity was not found. 

 

Isn't the Attribute and Entity the same thing? 

frahaman7SKBG_0-1662236072171.png

 

 

 

    Dim doc As Document
    doc = ThisApplication.ActiveDocument 

    ' Get the attribute manager.
    Dim attribMgr As AttributeManager
   attribMgr = doc.AttributeManager

    ' Find all attribute sets named "SampleSet".
    Dim foundEntities As ObjectCollection
  	foundEntities = attribMgr.FindObjects("hjalte.nl")

    MsgBox ("Found" &foundEntities.Count & " entities.")

 

0 Likes
Message 7 of 10

JelteDeJong
Mentor
Mentor

Entities are attributes with special (set)names.

JelteDeJong_0-1662237381536.png

 

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 8 of 10

frahaman7SKBG
Advocate
Advocate

I think I might have explained my problem incorrectly in the initial post. I want to use iLogic to inspect the model and Assign Entity Name on the biggest face. So instead of right click and 'Assign Entity Name', I want to do that with iLogic. Is that possible?

 

frahaman7SKBG_0-1662263918328.png

 

0 Likes
Message 9 of 10

JelteDeJong
Mentor
Mentor
Accepted solution

Oke creating entities is a bit more difficult. The script below will do it for you. Be aware that the entity is created but not directly visible. In some cases, you need to save and reopen the file. The script might also not work with faces of sheet metal features. If that is the case then you need to alter the script a bit to also loop over all sheet metal features while searching for the face.

Dim doc As PartDocument = ThisDoc.Document

Dim biggestFace = doc.ComponentDefinition.SurfaceBodies.
    Cast(Of SurfaceBody).
    SelectMany(Of Face)(Function(f) f.Faces.Cast(Of Face)).
    Aggregate(Function(face, nextFace) If(face.Evaluator.Area > nextFace.Evaluator.Area, face, nextFace))

Dim nameOfYourFace = "www.hjalte.nl"
Dim attSetName = "iLogicEntityNameSet"
Dim attName = "iLogicEntityName"
Dim attNameFeatureName = "iLogicEntityNameFeatureName"
Dim EntityNameSet = "iLogicEntityNameSet"
Dim entityNamesOrdered = "iLogicEntityNamesOrdered"

' Create Name attribute
Dim attSet As AttributeSet
If (biggestFace.AttributeSets.NameIsUsed(attSetName)) Then
    attSet = biggestFace.AttributeSets.Item(attSetName)
Else
    attSet = biggestFace.AttributeSets.Add(attSetName)
End If

If (attSet.NameIsUsed(attName)) Then
    attSet.Item(attName).Value = nameOfYourFace
Else
    attSet.Add(attName, ValueTypeEnum.kStringType, nameOfYourFace)
End If

' Create Feature attribute
Dim feature = Nothing
For Each cFeature As PartFeature In doc.ComponentDefinition.Features
    If cFeature.Faces Is Nothing Then Continue For
    For Each cFace As Face In cFeature.Faces
        If (biggestFace Is cFace) Then
            feature = cFeature
        End If
    Next
Next

Dim lCont As Long = doc.ReferenceKeyManager.CreateKeyContext()
Dim bKey() As Byte = New Byte() {}
feature.GetReferenceKey(bKey, lCont)
Dim strKey As String = doc.ReferenceKeyManager.KeyToString(bKey)

If (attSet.NameIsUsed(attNameFeatureName)) Then
    attSet.Item(attNameFeatureName).Value = strKey
Else
    attSet.Add(attNameFeatureName, ValueTypeEnum.kStringType, strKey)
End If

' Add entity to list of entities.

If (doc.AttributeSets.NameIsUsed(EntityNameSet)) Then
    attSet = doc.AttributeSets.Item(EntityNameSet)
Else
    attSet = doc.AttributeSets.Add(EntityNameSet)
End If

If (attSet.NameIsUsed(entityNamesOrdered)) Then
    Dim nameListString As String = attSet.Item(entityNamesOrdered).Value

    If (Not nameListString.Contains(nameOfYourFace)) Then
        nameListString = nameListString.Trim()
        Dim ents = nameListString.Split(vbTab).ToList()
        ents.Add(nameOfYourFace)
        attSet.Item(entityNamesOrdered).Value = String.Join(vbTab, ents)
    End If
Else
    attSet.Add(entityNamesOrdered, ValueTypeEnum.kStringType, nameOfYourFace)
End If

 

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

Message 10 of 10

frahaman7SKBG
Advocate
Advocate

Thank you @JelteDeJong 

0 Likes