I create a new virtual part in an assembly and then set the color of the body I just created using the MaterialProperyValues2. The color change shows up fine until the part is saved and then re-opened. Now the color reverts back to the default part color. Is this a bug? If so, is there a work around? Is there a better way to do this?
Here is my code if you wish to repeat the issue. You will need to select a face of an existing part before running the code for the routine to work:
public void TestColorIssue()
{
//save the selected face
Face2 fc = (Face2)swSelMgr.GetSelectedObject6(1, -1);
//create new part
Component2 newPart;
swAssy.InsertNewVirtualPart(null, out newPart);
newPart.Select4(false, null, false);
int info = 0;
swAssy.EditPart2(true, false, info);
//*** create a sketch off of all the edges in the selected face and extrude the sketch as a surface
SelectData data = null;
Loop2 lp = null;
object[] loops = (object[])fc.GetLoops();
if (loops != null)
{
for (int l = 0; l < loops.Length; l++)
{
lp = (Loop2)loops[l];
if (lp.IsOuter())
{
break;
}
}
}
//set the sketch plane to the face corresponding to this loop
Entity entLF = (Entity)fc;
entLF.Select4(false, data);
SketchManager sketchManager = modelDoc.SketchManager;
sketchManager.InsertSketch(true);
Sketch sketch = modelDoc.SketchManager.ActiveSketch;
modelDoc.ClearSelection2(true);
//select all the edges for this loop
bool boolStatus;
Edge edge;
Entity entEdge;
object[] edges = (object[])lp.GetEdges();
if (edges != null)
{
for (int j = 0; j < edges.Length; j++)
{
edge = (Edge)edges[j];
entEdge = (Entity)edge;
boolStatus = entEdge.Select4(true, data);
}
}
//create the sketch constrained to these edges
sketchManager.SketchUseEdge2(false);
// Create a blind surface extrude to the depth of the feature
FeatureManager myFeatMr = default(FeatureManager);
myFeatMr = modelDoc.FeatureManager;
Feature sketchFeature = (Feature)sketch;
String sketchName = sketchFeature.Name;
boolStatus = sketchFeature.Select2(false, 0);
myFeatMr.FeatureExtruRefSurface2(true, false, true, 0, 0, 0.5, 0.5, false, false, false,
false, 0.01745329251994, 0.01745329251994, false, false, false, false, false, false, false,
false);
//clear selection
modelDoc.ClearSelection2(true);
//***
swAssy.EditAssembly();
modelDoc.ForceRebuild3(true);
//** now go through all the bodies in the part we just created and color them red
object bodyInfo;
double[] color = new double[9] {1,0,0,1,1,1,0.3,0,0 };
object[] bodies = (object[])newPart.GetBodies3((int)swBodyType_e.swAllBodies, out bodyInfo);
if (bodies != null && bodies.Length > 0)
{
for (int i = 0; i < bodies.Length; i++)
{
Body2 body = (Body2)bodies[i];
body.MaterialPropertyValues2 = color;
}
}
}
SolidworksApi macros
