Problem with assigning material to a face using TessellatedShapeBuilder

Problem with assigning material to a face using TessellatedShapeBuilder

manvithJDVRS
Explorer Explorer
657 Views
3 Replies
Message 1 of 4

Problem with assigning material to a face using TessellatedShapeBuilder

manvithJDVRS
Explorer
Explorer

I am trying to assign two different material to faces using TessellatedShapeBuilder.

This works well when setting the target as TessellatedShapeBuilderTarget.Solid but fails for TessellatedShapeBuilderTarget.Mesh.

What might be the problem?

Here is the code

public class Test : IExternalCommand
{
    public Result Execute(
        ExternalCommandData commandData,
        ref string message,
        ElementSet elements)
    {
        UIApplication uiApp = commandData.Application;
        Application app = uiApp.Application;
        Document doc = uiApp.ActiveUIDocument.Document;

        string materialName = "Glass";
        string materialName2 = "Default";

        Material mat = (from material in new FilteredElementCollector(doc).
                          OfClass(typeof(Material)).
                          Cast<Material>()
                        where (material.Name == materialName)
                        select material).First();

        Material mat2 = (from material in new FilteredElementCollector(doc).
                          OfClass(typeof(Material)).
                          Cast<Material>()
                        where (material.Name == materialName2)
                        select material).First();
        CreateTessellatedShape(doc, mat.Id, mat2.Id);
        return Result.Succeeded;
    }

    // Create a pyramid-shaped DirectShape using given material for the faces
    public void CreateTessellatedShape(Document doc, ElementId materialId, ElementId matid2)
    {
        List<XYZ> loopVertices = new List<XYZ>(4);

        TessellatedShapeBuilder builder = new TessellatedShapeBuilder();

        builder.OpenConnectedFaceSet(true);
        // create a pyramid with a square base 4' x 4' and 5' high
        double length = 4.0;
        double height = 5.0;

        XYZ basePt1 = XYZ.Zero;
        XYZ basePt2 = new XYZ(length, 0, 0);
        XYZ basePt3 = new XYZ(length, length, 0);
        XYZ basePt4 = new XYZ(0, length, 0);
        XYZ apex = new XYZ(length / 2, length / 2, height);

        loopVertices.Add(basePt1);
        loopVertices.Add(basePt2);
        loopVertices.Add(basePt3);
        loopVertices.Add(basePt4);
        builder.AddFace(new TessellatedFace(loopVertices, materialId));

        loopVertices.Clear();
        loopVertices.Add(basePt1);
        loopVertices.Add(apex);
        loopVertices.Add(basePt2);
        builder.AddFace(new TessellatedFace(loopVertices, matid2));

        loopVertices.Clear();
        loopVertices.Add(basePt2);
        loopVertices.Add(apex);
        loopVertices.Add(basePt3);
        builder.AddFace(new TessellatedFace(loopVertices, materialId));

        loopVertices.Clear();
        loopVertices.Add(basePt3);
        loopVertices.Add(apex);
        loopVertices.Add(basePt4);
        builder.AddFace(new TessellatedFace(loopVertices, matid2));

        loopVertices.Clear();
        loopVertices.Add(basePt4);
        loopVertices.Add(apex);
        loopVertices.Add(basePt1);
        builder.AddFace(new TessellatedFace(loopVertices, materialId));

        builder.CloseConnectedFaceSet();
        builder.Target = TessellatedShapeBuilderTarget.Solid;
        builder.Fallback = TessellatedShapeBuilderFallback.Abort;
        // Does not work if
        // builder.Target = TessellatedShapeBuilderTarget.Mesh;
        // builder.Fallback = TessellatedShapeBuilderFallback.Salvage;

        builder.Build();

        TessellatedShapeBuilderResult result = builder.GetBuildResult();

        using (Transaction t = new Transaction(doc, "Create tessellated direct shape"))
        {
            t.Start();

            DirectShape ds = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_GenericModel));
            ds.ApplicationId = "Application id";
            ds.ApplicationDataId = "Geometry object id";

            ds.SetShape(result.GetGeometricalObjects());
            t.Commit();
        }
    }

As you can see above I have changed the materialIds alternatively for the faces. But for TessellatedShapeBuilderTarget.Mesh all faces have the same material.

I have attached screenshots for your reference.formesh.pngforsolid.png

 

0 Likes
658 Views
3 Replies
Replies (3)
Message 2 of 4

JimJia
Alumni
Alumni

Dear manvithJDVRS,

It is strange that only "Mesh/Salvage" cannot work.

Here is from API document:"Currently only "Solid/Abort", "AnyGeometry/Mesh" and "Mesh/Salvage" target/fallback combinations are supported"

 

I am communicating with engineer team, will keep you updated.

 

Thanks for your patience.


Jim Jia
Autodesk Forge Evangelist
https://forge.autodesk.com
Developer Technical Services
Autodesk Developer Network
Email: Jim.Jia@autodesk.com
0 Likes
Message 3 of 4

manvithJDVRS
Explorer
Explorer

Thank you for your reply.

Is there any update on this issue?

0 Likes
Message 4 of 4

JimJia
Alumni
Alumni

Sorry,

No response from engineer team now, I will keep an eye on it.

 


Jim Jia
Autodesk Forge Evangelist
https://forge.autodesk.com
Developer Technical Services
Autodesk Developer Network
Email: Jim.Jia@autodesk.com
0 Likes