Manifold Geometry // Многообразная Геометрия

Reset colors in a CAD/XDE model

/ Просмотров: 549

When we discussed how to colorize faces coming from a STEP file, our use case was communicating threads for CAM. There's one remark to this method though. What if the original model has some design colors already baked in? Apparently, these colors are going to mess up with the ones we might wanna put automatically. One easy workaround here is to reset all design colors before going further with our coloring "service." That's our topic for today.

While this ammendment is seemingly trivial, let's not forget that we're using that lovely XDE as a data structure. As a result, a pure technical question arises: how to reset all colors in a model with the provided XDE API?

Colored ANC101.

To start off, let's load a colored STEP file (I use ANC101) into an XDE document. In Analysis Situs, that's accomplished by the following commands:

1> asm-xde-load -model M -filename ANC101_colored.stp

2> asm-xde-dfbrowse -model M

The second line is optional but useful. With the help of this command, you invoke the DF Browser tool to inspect all labels and attributes of your OCAF model.

XDE document of a single part with colored faces.

In an XDE document, colors are separate entities, i.e., they are not stored as internal properties of solids. While this design decision of XDE is a bit unusual, it brings several advantages:

  1. Compactness. To have several faces (or parts) of the same color, XDE will not replicate the corresponding r-g-b values for each occurrence many times. You will rather end up having more and more links from a single existing color entity to all geometric primitives where it is used.

  2. Palette. By iterating this "Colors" section of the XDE document, you can easily collect all colors that are used in your design. We have exploited this possibility for implementing our "intelligent" color picker back in the days. Our color picker was supplemented with a custom palette prepopulated with all colors existing in the incoming part. Selecting a color from such a palette can be sometimes preferred over direct pixel picking as the latter depends on lightning and does not provide ab initio color.

  3. Easy cleanup. That's what we need: just go over all the labels in the "Colors" section and nullify them.
"Smart" color picker uses the ab initio design colors in its predefined palette.

To manipulate with colors, OpenCascade provides a specific tool named XCAFDoc_ColorTool. Although you can always work with an XDE document through its generic OCAF API (to add and remove attributes, for example), it is kinda recommended to utilize these "tool" interfaces from the XCAFDoc package if you wanna feel secured. So, to clean up all colors we put it like this:

void ResetColors()
{
  // Get color tool.
  Handle(XCAFDoc_ColorTool)
    CT = XCAFDoc_DocumentTool::ColorTool( m_doc->Main() );
  
  TDF_LabelSequence colorLabs;
  CT->GetColors(colorLabs);
  
  for ( TDF_LabelSequence::Iterator lit(colorLabs); lit.More(); lit.Next() )
  {
    TDF_Label colorLab = lit.Value();
    CT->RemoveColor(colorLab);
  }
}

What this code does is simply collecting all existing colors as OCAF labels and cleaning up their attributes (references to geometry). The following picture illustrates how a model looks like after cleaning up its colors:

XDE model after clean up.

To see this code in action, the following scenario can be used in Analysis Situs (ver. 1.2):

1> asm-xde-load -model M -filename ANC101_colored.stp

2> asm-xde-reset-colors -model M

3> asm-xde-save-step -model M -filename ANC101_uncolored.stp

The 3-rd line exports the modified XDE document to a STEP file. Without colors cleaned up, the outcome file will contain the original part without any visible changes. With the 2-nd line done, the model looses its per-part and per-element colors getting the default color of the software it's loaded in.

ANC101 with all colors cleaned up.

That's it. Hopefully, this article makes a little contribution to "democratizing" our lovely XDE framework that is quite Okay if you know how to handle it.