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.

Blog: hjalte.nl - github.com