Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Help add and get Attribute with iLogic

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
Anonymous
3576 Views, 9 Replies

Help add and get Attribute with iLogic

Dear All,

 

I am struggling for a long time with Attributes in Inventor. I managed to get some values from the attributes but I can't control which values to get and about adding I don't know anything (Below a small code to get some info). But on all the forums and website they use very large codes to do this. Dim This as That etc. and making the location smaller with oCode = Inventor.some.some.some (I try to avoid this if possible)

 

Dim oList As Collection = New Collection

For Each oattribute In ThisDoc.Document.attributesets

oList.Add(oattribute.Name)

Next

If oList.Count = 0 Then
MessageBox.Show("Empty", "ParameterList")
Return

Else

For Each st In oList

sTotal = sTotal + st & vbNewLine

Next

End If

 

MessageBox.Show(sTotal,"AttributeList")

 

In a Part I just want to add the Partname as Value to the Attributes and at a later point Get this attribute back. Preferably with the most short code possible. Any Ideas?

9 REPLIES 9
Message 2 of 10
Mark.Lancaster
in reply to: Anonymous

@Anonymous

 

Programming needs, issues and questions for Inventor need to be posted in the Inventor Customization forum for the best results.  I will have the moderator relocate your posting there to better suit your needs.

 

https://forums.autodesk.com/t5/inventor-customization/bd-p/120

Mark Lancaster


  &  Autodesk Services MarketPlace Provider


Autodesk Inventor Certified Professional & not an Autodesk Employee


Likes is much appreciated if the information I have shared is helpful to you and/or others


Did this resolve your issue? Please accept it "As a Solution" so others may benefit from it.

Message 3 of 10

@Anonymous,

 

Hoping that suggestions in below blog link may be helpful. same VB.net code can be used in iLogic.

 

http://adndevblog.typepad.com/manufacturing/2013/01/inventor-set-attributes-through-api.html

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 4 of 10
Anonymous
in reply to: chandra.shekar.g

I've already found that blog. But it is in my opinion much to complicated for what I need.

Message 5 of 10
bradeneuropeArthur
in reply to: Anonymous

Hi,

 

What are you trying to do exactly?

Regards,

Arthur Knoors

Autodesk Affiliations:

Autodesk Software:Inventor Professional 2024 | Vault Professional 2022 | Autocad Mechanical 2022
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:Drawing List!|Toggle Drawing Sheet!|Workplane Resize!|Drawing View Locker!|Multi Sheet to Mono Sheet!|Drawing Weld Symbols!|Drawing View Label Align!|Open From Balloon!|Model State Lock!
Posts and Ideas:Dimension Component!|Partlist Export!|Derive I-properties!|Vault Prompts Via API!|Vault Handbook/Manual!|Drawing Toggle Sheets!|Vault Defer Update!


! For administrative reasons, please mark a "Solution as solved" when the issue is solved !

Message 6 of 10
Anonymous
in reply to: bradeneuropeArthur

As stated in my post: "In a Part I just want to add the Partname as Value to the Attributes and at a later point Get this attribute back. Preferably with the most short code possible. Any Ideas?"

Message 7 of 10
HermJan.Otterman
in reply to: Anonymous

Hello M,

 

below comes from the help in Inventor, and with that code you can create and change attributes.

why are there so many dim's this and thats..?? they are needed to let your code work fine without stopping all the time for many reasons. most other code is error handeling.

 

so try the code from the previous examples or from the examples below,

just the shortest code will get you in trouble.

 

and still, what would you like to accomplish is not clear? just to put the name in an attribute? why? if you can access that name, why put it in a attrubute. if you explain a little more, then others can help you better..

 

Attributes

Attributes Introduction to Attributes and AttributeSets.

The Autodesk Inventor API provides the means to append data, in the form of named data or a name-value pair, to objects in Autodesk Inventor. Attributes are how the programmer stores nongraphical data on Autodesk Inventor objects, and for this data to be maintained by Autodesk Inventor. Attribute data is also accessible using Apprentice Server. Attribute data is persistent by default. However, if the Transient property is set to true, the data is to be maintained during the current Autodesk Inventor session only.

You can append the following types of data: integer, double, string, byte array, or any combination of these. Applications differentiate their attributes from those of other applications through the use of AttributeSets. An AttributeSet can contain any number of Attributes, and an object can contain any number of AttributeSets, dependant only on system resources. Most persistent objects (SketchLine, Constraint, Parameter, and so on) in Autodesk Inventor support Attributes. Brep objects support attributes in the current session, with attributes maintained across parametric changes and model re-computations.

There is no direct User Interface equivalent to Attributes. Similar API functionality exists in AutoCAD through Extended Entity Data, otherwise known as XED or xdata.

The purpose of Attributes

Attributes are commonly used to attach application-specific data to objects within Autodesk Inventor. A typical example is in referencing records in an external database, such as Microsoft Access or Oracle. An attribute appended to a part occurrence may contain a key value. An application can use this value to identify a unique database record (perhaps using ODBC or OLEDB). Thus Autodesk Inventor maintains an attribute value that is a persistent link to further data in an external database. This database can be used for Bill of Materials reports, data updates, and so on.

It is useful to be able to locate Autodesk Inventor objects containing attributes of a certain value. Use the AttributeManager object to accomplished this. The following object model diagrams show that this object is separate from the Attribute and AttributeSets objects. The AttributeManager object is obtained from the document object (DrawingDocument, PartDocument, AssemblyDocument, ApprenticeServerDocument and so on.)

AttributeSet and Attribute Object Model Diagram

Capture.PNG

AttributeManager Object Model Diagram

 

AttributeManager Object Model Diagram

 

Accessing Attributes through the API

To take full advantage of attributes, it is necessary to add, modify and erase Attributes and AttributeSets programmatically. The AttributeSets object also supports the DataIO object, allowing attribute data to be written out in XML form.

 

Note: Since Autodesk Inventor 9, Attributes and AttributeSets also support the Transient property. If this property is set to true, or if the AttributeSet is created using the AddTransient method of AttributeSets, the attribute data is not persisted beyond the current session. This is useful if you want to do some short-term data manipulation but do not wish to 'dirty' the document. For example, when working with Brep objects at the session level.

 

Creating an AttributesSet

The first step in appending data to an Autodesk Inventor object is to create and add a new AttributeSet object to the AttributeSets collection. This is obtained through the AttributeSets property of the object to which the attributes are to be attached. Initially, this AttributeSet can be empty of Attributes. So, assuming an assembly document contains a single part occurrence oMyPartOccurrence:

 

Dim oAttribSets As AttributeSets
Set oAttribSets = oMyPartOccurrence.AttributeSets

Dim oAttribSet As AttributeSet
    
On Error Resume Next
Set oAttribSet = oAttribSets.Add("MyAttribSet")
If Err Then
  Set oAttribSet = oAttribSets.Item("MyAttribSet")
End If

 

The preceding sample code obtains the AttributeSets collection object from the part occurrence, and then adds a new AttributeSet object named "MyAttribSet" to the collection, if one doesn't already exist.

Creating Attributes and adding or modifying values

Next, declare and add an Attribute to the AttributeSet object using the add method of the AttributeSet object.

 

Dim oAttrib As Inventor.Attribute
If oAttribSet.Count = 0 Then
  Set oAttrib = oAttribSet.Add("MyAttrib", kStringType, "Some Text")
End If

 

This code adds a single attribute of type String, containing the text "Some Text", after first checking that the AttributeSet doesn't already contain attributes. An alternative is to check if existing attributes have the same name, and if not, add the new attribute anyway since there is no naming conflict. To modify the value of an existing attribute, update its Value property.

Retrieving attributes values

When obtaining the value of an existing attribute, you usually know its name. The Item method of both AttributeSets allow the use of a name string to identify the required object. Alternatively, the Item method accepts a numeric index value, allowing iteration of all available AttributeSets. In this example, we use the previously named attributes.

 

Dim oAttribSets As AttributeSets
Set oAttribSets = oMyPart.AttributeSets

Dim oAttrib As Inventor.Attribute
Set oAttrib = oAttribSets.Item("MyAttribSet").Item("MyAttrib")
Debug.Print oAttrib.Value

 

For clarity and brevity, the previous example doesn't perform any error checking. You could use the NameIsUsed property (see the next section) to check whether the attribute exists. Given the existence of the attribute 'MyAttrib' this code prints out its value, "Some Text".

Deleting Attributes

Deleting attributes is straightforward, except that it is always wise to check for the existence of attributes contained in an AttributeSet before deleting that AttributeSet. If the AttributeSet contains attributes, delete those attributes before deleting the AttributeSet. It is the programmer's responsibility to ensure it's safe to delete attributes and AttributeSets. There is no Recycle Bin for attributes, although you can undo the last operation. If orphaned attributes exist due to the owning object being deleted or inaccessible, such attributes should be purged using the PurgeAttributeSets method of the AttributeManager.

The following code uses the delete method of the Attribute object, followed by the delete method of the parent AttributeSet object.

 

Dim oAttribSets As AttributeSets
Set oAttribSets = oMyPart.AttributeSets

If oAttribSets.NameIsUsed("MyAttribSet") Then
  Set oAttrib = oAttribSets.Item("MyAttribSet").Item("MyAttrib")
  oAttrib.Delete
  oAttribSets.Item("MyAttribSet").Delete
End If

 

Again, the previous code uses minimal error checking, though it does demonstrate the use of the NameIsUsed property to verify the existence of the AttributeSet.

Searching for Attributes using the AttributeManager object

An assembly may contain hundreds or thousands of objects, be they occurrences, dimensions, constraints, or any other persistent object. If all these objects contain attributes as described in the preceding sections, it would be a very slow and tedious process to search each object for a given AttributeSet or Attribute. Brep objects are another example, with a potentially large number of objects to iteration through. Thankfully, Autodesk Inventor provides an API to help - the AttributeManager object. You can obtain this object from the Document object, and from derived documents such as AssemblyDocument. The AttributeManager object provides a number of methods. For example, to find if an AttributeSet, Attribute or Attribute value exists, or to return objects based on specific attribute values.

 

Note: A helpful feature of the AttributeManager object is that wildcard characters are legal when used to specify AttributeSet or Attribute names. So, it is possible to perform searches for objects using partial name or value strings.

 

AttributeManager methods such as OpenAttributeSets return an AttributeSetsEnumerator object. This is a list of AttributeSets. The OpenAttributeSets method requires a list of objects for which you need AttributeSets. The AttributeSetsEnumerator returned has its AttributeSets ordered in the same way as your list of objects, therefore you can easily match which AttributeSet relates to which object by their numerical index. This technique can help to improve performance with large numbers of attributes.

The following code assumes a PartDocument object containing a single part, which in turn has faces to which attributes are attached. This example uses OpenAttributeSets to find AttributeSet "Test" on these face objects, and prints out the value of the first attribute in the AttributeSet. This would be much quicker than opening each face.

 

Dim oDoc As PartDocument
Set oDoc = ThisApplication.ActiveDocument

Dim oFaceColl As ObjectCollection
Set oFaceColl = ThisApplication.TransientObjects.CreateObjectCollection
For Each oFace In oDoc.ComponentDefinition.SurfaceBodies.Item(1).Faces
  oFaceColl.Add oFace
Next

Dim oSet As AttributeSet
Dim oSetEnum As AttributeSetsEnumerator

Set oSetEnum= oDoc.AttributeManager.OpenAttributeSets(oFaceColl,"Test")
For Each oSet In oSetEnum
  If oSet.Count > 0 Then
    Debug.Print oSet.Item(1).Value
  End If
Next

 

Other methods supported by the AttributeManager object include FindObjects. As its name suggests, this method returns a collection of objects corresponding to specified AttributeSet or Attribute names, or attribute values.

Performance

Conceptually, attributes are appended to persistent objects in Autodesk Inventor. In reality, the link is rather more tenuous. Autodesk Inventor is a parametric modeler; a uniform system for parts and assemblies. Objects in Inventor easily change, whether driven by parameters or as a result of a modeling operation such as the addition of a hole. So a persistent object such as a face is not necessarily as persistent or consistently identifiable as it first appears.

Autodesk Inventor has a complex search algorithm to enable it to consistently identify objects throughout their lifetime. This mechanism is employed by ReferenceKeys, a concept similar to handles in AutoCAD. This internal ReferenceKeys mechanism provides a reliable and robust link between an AttributeSet and the object to which it is appended. However, the link is potentially expensive in terms of processing time, particularly when you are working with large numbers of Attributes. There are several ways to mitigate the effects of this performance issue.

  • Autodesk Inventor builds a cache of object-to-AttributeSet links as these links are referenced. This cache greatly reduces access time for subsequent references. You can force Autodesk Inventor to build the entire cache, for all objects, by calling the FindObjects method with no arguments. Then perform your query. Note though that continued modeling operations will destroy the cache.
  • When working with large numbers of Attributes, enclose your Attribute editing in a Transaction or ChangeProcessor. This tells Autodesk Inventor not to use its own automatic transaction and undo mechanism, greatly improving performance.

Also consider

Attributes are often used in conjunction with ReferenceKeys. Use Referencekeys to obtain a persistent identifier for a given Autodesk Inventor object. This identifier is valid from session to session. For example, an application can establish a relationship between objects in an assembly by storing a ReferenceKey for one object in a persistent attribute on another.

If this answers your question then please select "Accept as Solution"
Kudo's are also appreciated Smiley Wink

Succes on your project, and have a nice day

Herm Jan


Message 8 of 10
Anonymous
in reply to: HermJan.Otterman

Dear Herm Jan,

 

Thank you for your message. 

 

I know why there are dim this and that's but there are a lot of people that seem to overuse those. I always try to avoid this kind of things if not necessary:

 

Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument

Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = oAsmDoc.ComponentDefinition

 

I know for these kind of things it is important:

 

Dim oList As Collection = New Collection

 

That's why I put that in my original post. 


----------------------------------------------------------------------------------------------

Oke now back to the question. 

As I've tried to explain in my post. I've already tried to get information into the Attributes with the use of forums but with no success. That's why I hoped that somebody can just give a simple code to add and get attribute values. If needed I can add error handling etc. myself.

---------------------------------------------------------------------------------------------------------------------

What I exactly try to do. I want to make a rule that knows if it has already showed some inputboxes. If I make a new part from another one I want the iLogic rule to only show 1 time some inputboxes. 

The way I want it to do is (very simple explained, will offcourse not work like this):

 

SyntaxEditor Code Snippet

'Check if new Partname is same as in attributes
If iProperties.Value("Custom", "Material Type") = "" Or Attribute("CheckFirstTimeMT").Value <> ThisDoc.FileName(False) Then 
oMaterial_Type = InputBox("Please Check Material Type", "Material Type", iProperties.Value("Custom", "Material Type"))
End If

'Put Materialtype to Custom iProperties.Value("Custom", "Material Type") = oMaterial_Type 'Fill in Current partname to attributes Attribute("CheckFirstTimeMT").Value = ThisDoc.FileName(False)

 

Kind regards,

 

 

 

 

Message 9 of 10
HermJan.Otterman
in reply to: Anonymous

Oke,

 

the shortest way to create an Attribute with iLogic in a component:

 

SyntaxEditor Code Snippet

Dim oDoc As Document = ThisDoc.Document

Dim MyAttribSet As AttributeSet = oDoc.AttributeSets.Add("MyAttributeSet")
 'this will give an error if it already exists!

Dim MyAttribName As Attribute = MyAttribSet.Add("Name", ValueTypeEnum.kStringType, "filename")
 'this will give an error if it already exists!

 but as said before, this wil only run once, and after that it will give errors, and you need code to handle that.

 

you also could use custom properties, like "FirstRun" or so, that way it is easier to see (and set) what the value is. with attributes you always need some code.

If this answers your question then please select "Accept as Solution"
Kudo's are also appreciated Smiley Wink

Succes on your project, and have a nice day

Herm Jan


Message 10 of 10
Anonymous
in reply to: HermJan.Otterman

Dear Herm Jan,

 

Thank you for your message.

 

This is exactly what I was looking for. Didn't get the basic idea of Attributes but it is a lot clearer now. Yes I've also considered doing this with Custom Properties but I think it will cause confusion to my other colleagues. 

 

For now I tried a very simple way to test. And it works.

 

SyntaxEditor Code Snippet

oAttributeValue = ThisDoc.Document.attributesets.item("Checks").item("CheckFirstTime").Value

MessageBox.Show(oAttributeValue, "Attributevalue")

 

So thank you!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report