Message 1 of 4
Problem with assigning material to a face using TessellatedShapeBuilder
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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.