Reputation: 21
I was successful enough to add a new wall (IfcWallStandardCase) to an existing IFC model and display it in the DrawingControl3D - with the help from Proper Wall 3D sample from XBim Toolkit, here's my code of adding colour to the wall:
//add color to the proper wall
var orange = model.Instances.New<IfcColourRgb>();
orange.Red = (255.0 / 255.0);
orange.Green = (69.0 / 255.0);
orange.Blue = (0.0 / 255.0);
var newStyleRendering = model.Instances.New<IfcSurfaceStyleRendering>();
newStyleRendering.SurfaceColour = orange;
var newSurfaceStyle = model.Instances.New<IfcSurfaceStyle>();
newSurfaceStyle.Styles.Add(newStyleRendering);
var newStyleAssignment = model.Instances.New<IfcPresentationStyleAssignment>();
newStyleAssignment.Styles.Add(newSurfaceStyle);
var newStyledItem = model.Instances.New<IfcStyledItem>();
newStyledItem.Name = "Standard Wall Styling";
newStyledItem.Item = body;
newStyledItem.Styles.Add(newStyleAssignment);
Now I'm trying to change the color of that wall upon a button click using how the style (colour) was added in the first place, here's my attempt to do it:
var walls = model.Instances.OfType<IfcStyledItem>();
var _newWall = walls.Where(w => w.Name == "Standard Wall Styling").FirstOrDefault();
if (_newWall != null)
{
var newColour = model.Instances.New<IfcColourRgb>();
newColour.Red = (24 / 255.0);
newColour.Green = (24 / 255.0);
newColour.Blue = (24 / 255.0);
var newStyleRendering = model.Instances.New<IfcSurfaceStyleRendering>();
newStyleRendering.SurfaceColour = newColour;
_newWall.Styles[0].SurfaceStyles.FirstOrDefault().Styles.Clear();
_newWall.Styles[0].SurfaceStyles.FirstOrDefault().Styles.Add(newStyleRendering);
txn.Commit();
DrawingControl.ReloadModel();
}
I get the named "IfcStyledItem" via name query, and traverse to it's "SurfaceStyle" to replace the "ColourRGB" that was set before, then reload the DrawingControl3D model. But not successful.
I've been looking around the net how to do this properly but couldn't find any. Any leads on how to achieve this is very much appreciated.
Upvotes: 2
Views: 1166
Reputation: 108
ok in order to do so u need to assign a new IfcSurfaceStyle
to your IfcExtrudedAreaSolid
of the wall.the code bellow works fine for Proper Wall 3D example and ifc export from revit 2018.
private bool AddNewSurfaceStyle(IfcBuildingElement element)
{
var representations = element.Representation.Representations;
if (representations.Count == 0) return false;
var body = representations.FirstOrDefault(a => a.RepresentationType == "SweptSolid");
if (body == null) return false;
var extrudedAreaSolid = body.Items.FirstOrDefault(a => a is IIfcExtrudedAreaSolid);
if (extrudedAreaSolid == null) return false;
var elementStyle = extrudedAreaSolid.StyledByItem.FirstOrDefault();
using (var txn = element.Model.BeginTransaction("Create Style"))
{
var styleAssignment = element.Model.Instances.New<IfcPresentationStyleAssignment>();
var surfaceStyle = element.Model.Instances.New<IfcSurfaceStyle>();
var surfaceStyleRedering = element.Model.Instances.New<IfcSurfaceStyleRendering>();
var colourRGB = element.Model.Instances.New<IfcColourRgb>();
colourRGB.Blue = 1;
colourRGB.Red = 1;
colourRGB.Green = 0;
surfaceStyleRedering.Transparency = 0;
surfaceStyleRedering.SurfaceColour = colourRGB;
surfaceStyle.Styles.Add(surfaceStyleRedering);
styleAssignment.Styles.Add(surfaceStyle);
elementStyle.Styles.Clear();
elementStyle.Styles.Add(styleAssignment);
txn.Commit();
}
return true;
}
Upvotes: 1