Attributes and Attribute Sets

Top  Previous  Next

An "Attribute" is extra data added to an Inventor object. You, the programmer, can store non graphical data in an object's attributes.

 

If you have ever programmed AutoCAD you could compare attributes in Inventor with Xdata in AutoCAD.

 

AutoDesk supplies a program called Attribute Helper which you can use to add attributes into objects. For example into the faces of a part.

 

Attribute-Set

 

 

Imagine the cylindrical surface of the part above has some attribute sets. This is how to list them programatically:

 

   CComQIPtr<PartDocument> pPartDoc ; // will remain nullptr if no part found

 

   // Find the documents which are currently open...

   CComPtr<Documents> pDocs  ;

   HRESULT hRes = theApp.GetInvAppPtr()->get_Documents (&pDocs) ;

   if (FAILED(hRes) || (pDocs == nullptr)) {

       ShowCOMError(hRes,L"CCollettore::CreateCollectorMainTubePart,get_Documents failed ");

       return  ;

   }

 

   const int ikNumDocs = pDocs->Count ;

 

 

   // Get hold of the first Part doc which you find...

   for (int iDoc = 1 ; iDoc <= ikNumDocs ; iDoc++) {

 

      CComPtr<Document> pDocument  ;

 

      hRes = pDocs->get_Item(iDoc,&pDocument) ;

 

      if (FAILED(hRes) || (pDocument == nullptr)) {

           TRACE (L"Could not get document number %d",iDoc);

           return ;

      }

 

      TRACE (L"Got document %d of type %d %s\n",iDoc,pDocument->DocumentType,

                                                GetInventorDocTypeDesc(pDocument->DocumentType));

 

      if (pDocument->DocumentType == kPartDocumentObject) {

          // Convert from the "general document" type into the "drawing document" type...

          pPartDoc = pDocument ;

          break ; // Assume there is only one

      }

   }

 

   if (pPartDoc == nullptr) {

       WalertBoxA ("No part found") ;

       return ;

   }

 

   // Get hold of the component defintion so you can look at the faces..

   CComPtr<PartComponentDefinitions> pPartCompDefs ;

   pPartDoc->get_ComponentDefinitions (&pPartCompDefs) ;

 

   TRACE (" there are %d component defs\n",pPartCompDefs->Count) ;

 

   CComPtr<PartComponentDefinition> pPartCompDef ;

   pPartCompDefs->get_Item (1,&pPartCompDef) ;

 

   CComPtr<SurfaceBodies> pSurfaceBodies;

   hRes = pPartCompDef->get_SurfaceBodies(&pSurfaceBodies);

 

   // Get the first surface body from the component definition.

   // (There should only ever be one surface body.)

   CComPtr<SurfaceBody> pBody;

   hRes = pSurfaceBodies->get_Item(1, &pBody);

 

   // Get the shells from the body.

   CComPtr<FaceShells> pShells;

   hRes = pBody->get_FaceShells(&pShells);

 

   // Enumerate through the shells.

   const long ikNumShells = pShells->Count;

   for (long iShell = 1 ; iShell <= ikNumShells ; iShell++)   {

       CComPtr<FaceShell> pShell;

       hRes = pShells->get_Item(iShell, &pShell);

 

       TRACE (L"  Shell %d\n", iShell);

 

       // Get the faces from the shell.

       CComPtr<Faces> pFaces;

       hRes = pShell->get_Faces(&pFaces);

 

       // Enumerate through the faces of the current shell.

       const long ikNumFaces = pFaces->Count;

       for (long iFace = 1 ; iFace <= ikNumFaces ; iFace++) {

           CComPtr<Face> pFace;

           hRes = pFaces->get_Item(iFace, &pFace) ;

 

           CComPtr <Edges> pEdges  ;

           pFace->get_Edges (&pEdges) ;

 

           CComPtr<AttributeSets> pFaceAttSets  ;

           pFace->get_AttributeSets (&pFaceAttSets) ;

           TRACE ("Face %d has %d attribute sets\n",iFace,pFaceAttSets->Count) ;

 

           // This face has at least one attribute set...

           if (pFaceAttSets->Count > 0) {

 

               // Get the first attribute set...

               CComPtr<AttributeSet> pFaceAttSet  ;

               pFaceAttSets->get_Item (CComVariant(1),&pFaceAttSet) ;

 

               if (pFaceAttSet != nullptr) {

 

                   // Get the first attribute...

                   CComPtr<Attribute> pFaceAtt  ;

                   pFaceAttSet->get_Item (CComVariant(1),&pFaceAtt) ;

 

                   // Show the user what is inside the attribute...

                   if (pFaceAtt != nullptr) {

                       CComBSTR cbstrName ;

                       pFaceAtt->get_Name (&cbstrName) ;

 

                       // Get the value. Use a CComVariant because there are strings

                       // and number variants...

                       CComVariant varValue ;

                       pFaceAtt->get_Value (&varValue) ;

 

                       // Here we assume it is a string...

                       WalertBoxW (L"Name = <%s> Value = <%s>",cbstrName,varValue.bstrVal) ;

                   }

               }

           }

       }

   }

 

 

 

 

 

 

Text, images and diagrams © 2021 Owen F. Ransen. All rights reserved. (But copy the source code as much as you want!)