C SDK - SUEdgeSetColor seems to create a new <auto> material for every call

I am trying to create 3 edges and I have created 2 colors, and two materials one for each color referencing the same SUColor objects.

When I call the function

SUEdgeSetColor(edge1, &color1);

This ends up creating a new material which ends up being called autox where x is an auto incrementing number for every call to the SUEdgeSetColor() function.

What am I doing wrong?

color

Full Source Below

#include <stdio.h>
#include <iostream>
#include <SketchUpAPI/common.h>
#include <SketchUpAPI/geometry.h>
#include <SketchUpAPI/initialize.h>
#include <SketchUpAPI/model/model.h>
#include <SketchUpAPI/model/entities.h>
#include <SketchUpAPI/model/face.h>
#include <SketchUpAPI/model/material.h>
#include <SketchUpAPI/model/edge.h>
#include <SketchUpAPI/model/vertex.h>

int main() {
  // Always initialize the API before using it
  SUInitialize();
  // Create an empty model
  SUModelRef model = SU_INVALID;
  SUResult res = SUModelCreate(&model);
  // It's best to always check the return code from each SU function call.
  // Only showing this check once to keep this example short.
  if (res != SU_ERROR_NONE)
    return 1;
  // Get the entity container of the model
  SUEntitiesRef entities = SU_INVALID;
  SUModelGetEntities(model, &entities);
  
  
  // Create Color #1
  SUColor color1 = SUColor();
  color1.alpha = 255;
  color1.red = 255;
  color1.green = 0;
  color1.blue = 0;
  
  // Create Color #2
  SUColor color2 = SUColor();
  color2.alpha = 255;
  color2.red = 0;
  color2.green = 0;
  color2.blue = 255;

  // Create Material #1
  SUMaterialRef material1 = SU_INVALID;
  SUMaterialCreate(&material1);
  SUMaterialSetName(material1, "MyColor1");
  SUMaterialSetColor(material1, &color1);
  SUMaterialSetOpacity(material1, 1);
  SUMaterialSetUseOpacity(material1, true);
  SUMaterialSetType(material1, SUMaterialType::SUMaterialType_Colored);

  // Create Material #2
  SUMaterialRef material2 = SU_INVALID;
  SUMaterialCreate(&material2);
  SUMaterialSetName(material2, "MyColor2");
  SUMaterialSetColor(material2, &color2);
  SUMaterialSetOpacity(material2, 1);
  SUMaterialSetUseOpacity(material2, true);
  SUMaterialSetType(material2, SUMaterialType::SUMaterialType_Colored);
  
  // Add Materials
  SUMaterialRef materials [] = { material1, material2 };
  SUModelAddMaterials(model, 2, &materials[0]);


  // Create Edge #1
  SUEdgeRef edge1 = SU_INVALID;
  SUPoint3D start1 = { 0,0,0 };
  SUPoint3D end1 = { 5,5,5 };
  SUEdgeCreate(&edge1, &start1, &end1);
  SUEdgeSetColor(edge1, &color1);

  // Create Edge #2
  SUEdgeRef edge2 = SU_INVALID;
  SUPoint3D start2 = { 2,2,2 };
  SUPoint3D end2 = { 11,2,3 };  
  SUEdgeCreate(&edge2, &start2, &end2);
  SUEdgeSetColor(edge2, &color1);

  // Create Edge #3
  SUEdgeRef edge3 = SU_INVALID;
  SUPoint3D start3 = { 3,3,0 };
  SUPoint3D end3 = { 9,9,0 };
  SUEdgeCreate(&edge3, &start3, &end3);
  SUEdgeSetColor(edge3, &color2);
  
  // Add Edges
  SUEdgeRef edges[] = { edge1, edge2, edge3 };
  SUEntitiesAddEdges(entities, 3, &edges[0]);
  
  // Save Model
  res = SUModelSaveToFile(model, "new_model.skp");

  // Must release the model or there will be memory leaks
  SUModelRelease(&model);
  // Always terminate the API when done using it
  SUTerminate();

  if (res != SU_ERROR_NONE) {
    std::cout << "Unable to save document!";
    return 1;
  }
  
  std::cout << "Document saved!";
  return 0;
}

I don’t know.

Maybe you need to convert the Edge to a Drawingelement, then use SUDrawingElementSetMaterial.

Thanks Jim

Do you know how to cast the Edge to a DrawingElement?

My feeble attempts have failed so far.

Here’s what worked for me:

        SUDrawingElementRef de = SUEdgeToDrawingElement(edge3);
	SUDrawingElementSetMaterial(de, material2);

So don’t set the Edge color directly. Convert the Edge to a DrawingElement, then apply the material to the drawing element.

I’m not sure if the “auto” naming of colors is considered a bug or not.

1 Like

Thank you very much, this works fine.

Cheers

Chris