Author Topic: [AcBr(4)]Set a different color for each face of 3DSOLID  (Read 878 times)

0 Members and 1 Guest are viewing this topic.

xdcad

  • Bull Frog
  • Posts: 424
[AcBr(4)]Set a different color for each face of 3DSOLID
« on: November 16, 2023, 10:02:13 PM »



We know that there is no command in CAD to set a different color for each face of 3DSOLID. We can do it through the ACBR API.

Using ACBRAPI, iterate over each face (FACE) and set the color of each face by the child entity ID of the child entity path

=============

What is "ACBR API"?


AcBr API is ARX's application development interface for processing topological objects. It is used to process the topological relationships of three-dimensional geometric entities such as AcDbRegion, AcDb3DSolid, and AcDbBody.
Except for ARX, other development interfaces such as VLISP do not provide encapsulated interfaces.


Through this API, you can easily query the data and topological relationships of "volumes, surfaces, rings, edges, and points" of geometric model entities.
AcBrEntity is the base class of this interface and cannot be instantiated by itself. It derives subclasses such as AcBrBrep, AcBrFace, AcBrLoop, AcBrEdge, AcBrVertex and AcBrShell. And through the topology traverser AcBrTraverser class, the entire model entity is traversed in the order of body, surface, ring, edge, and point.

==============

Here is the ARX code:

Code - C++: [Select]
  1. void TestBrep(void)
  2. {
  3.     Adesk::Int32 len;
  4.     ads_name  ssname0;
  5.   struct resbuf *buffer;
  6.     buffer = acutBuildList(-4, _T("<AND"),
  7.               RTDXF0, _T("3DSOLID"),
  8.               -4, _T("AND>"), RTNONE);
  9.     acutPrintf(_T("\nSelect a box:"));
  10.     acedSSGet(NULL, NULL, NULL, buffer, ssname0);
  11.    acutRelRb(buffer);
  12.   if (RTNORM == acedSSLength(ssname0, &len))
  13.     {
  14.         ads_name  ent;
  15.         AcDbObjectId entId;
  16.     for(long k = 0; k < len; k++)
  17.         {
  18.             acedSSName(ssname0, k, ent);
  19.             acdbGetObjectId(entId, ent);
  20.       settingDifferentColorToEachFace(entId);
  21.         }
  22.         acedSSFree(ssname0);
  23.     }
  24. }
  25.  
  26. void settingDifferentColorToEachFace(AcDbObjectId solidId)
  27. {
  28.   AcCmColor specialColor;
  29.   AcDb3dSolid* pSolid;
  30.     if (Acad::eOk == acdbOpenObject(pSolid, solidId, AcDb::kForRead))
  31.     {
  32.         AcDbFullSubentPath path(solidId, AcDbSubentId());
  33.     AcBrBrep brep;
  34.         AcBr::ErrorStatus bs = brep.setSubentPath(path);
  35.         if (bs != AcBr::eOk)
  36.             return;
  37.    //Initialize the BrepFace traverser
  38.         AcBrBrepFaceTraverser bft;
  39.         bs = bft.setBrep(brep);
  40.         if (bs != AcBr::eOk)
  41.             return;  
  42.         AcArray<AcDbSubentId> arrSubentId;
  43.         // Traverse all faces
  44.         for (;!bft.done();bft.next())
  45.         {
  46.             AcBrFace face;
  47.             bs = bft.getFace(face);    
  48.             if (bs != Acad::eOk)
  49.             {
  50.                 acutPrintf(L"\ngetFace failed");
  51.                 break;
  52.             }
  53.             AcDbFullSubentPath    Path(kNullSubent);
  54.             AcDbSubentId          subentId;
  55.             AcBr::ErrorStatus bss = face.getSubentPath(Path);
  56.             subentId = Path.subentId();
  57.             arrSubentId.append(subentId);
  58.         }
  59.         pSolid->upgradeOpen();
  60.         for (int i = 0; i < arrSubentId.length(); i++)
  61.         {
  62.       specialColor.setColorIndex(i);
  63.             pSolid->setSubentColor(arrSubentId[i],specialColor);
  64.         }
  65.         pSolid->downgradeOpen();
  66.     }
  67.     pSolid->close();
  68. }
  69.  

===============

The following is the LISP code implemented by the AcBr library function of the XDRX API:
The code is as follows: three-layer loop traversal structure.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:tt ()
  2.    (defun _traversface (e)
  3.      (setq br (xdbr::constructor e))
  4.      (setq tr (xdbr::constructor "brepfacetraverser" br))
  5.      ;;FACE traversal
  6.      (setq ids nil)
  7.      (while (not (xdbr::traverser:done tr));Traverse the face
  8.        (if (setq face (xdbr::getpropertyvalue tr "face")); Get the face AcBrFace at the current traversal pointer position
  9.          (progn (setq SubEntPath (xdbr::getpropertyvalue face "subentpath");Get the subentity path of the face
  10.                       SubEntId (xdrx_getpropertyvalue SubEntPath "subentid");;Get the subentity ID
  11.                       ids (cons SubEntId ids);save to global table
  12.                 )
  13.                 (xdrx_object_release SubEntPath);;Release the subentity path variable
  14.          )
  15.        )
  16.        (xdbr::traverser:next tr);;The traverser points to the next position
  17.      )
  18.      (xdrx_object_release tr br);;Release the traverser and BREP variables
  19.      (setq i 0)
  20.      (repeat (length ids);Set the color of the fruit body surface
  21.        (xdrx_setpropertyvalue
  22.          e
  23.          "subentcolor"
  24.          (list (setq SubEntId (nth i ids)) (setq i (1+ i)))
  25.        )
  26.        (xdrx_object_release SubEntId);;Release the subentity ID variable
  27.        (xdrx_prompt
  28.          (xdrx_string_format "\n - Face[%d]-Color[%d]" i i)
  29.        )
  30.      ) ;
  31.    )
  32.    (if (setq e (car (xdrx_entsel "\nSelect 3DSOLID<Exit>:" '((0 . "3DSOLID"))))
  33.                )
  34.        )
  35.      (progn (xdrx_begin) (_traversface e) (xdrx_end))
  36.    )
  37.    (princ)
  38. )

=============

The above LISP code uses the XDRX-API, which can be downloaded from https://github.com/xdcad/XDrx-API

The XDRX API encapsulates AcDb, AcEd, AcGe, AcBr... C++ library, using C++ methods to develop LISP programs.Thousands of Lisp functions are available.


The code I wrote uses XDRX-API,which can be downloaded from github.com and is updated at any time.
===================================
[XDrx-Sub Forum]
https://www.theswamp.org/index.php?board=78.0
https://github.com/xdcad/XDrx-API
http://bbs.xdcad.net