xBimTeam / XbimEssentials

A .NET library to work with data in the IFC format. This is the core component of the Xbim Toolkit
https://xbimteam.github.io/
Other
477 stars 171 forks source link

Changing color of IfcBuildingElementProxy #552

Open wojtpl opened 4 months ago

wojtpl commented 4 months ago

I want to set color to my existing ifc file by element name. I stucked on " Cannot implicitly convert type 'Xbim.Ifc2x3.ProductExtension.IfcBuildingElementProxy' to 'Xbim.Ifc2x3.GeometryResource.IfcRepresentationItem' " Please give me a clue or an example how should I fix it.

#

using (var model = IfcStore.Open(fileName))
{

    var element = model.Instances.FirstOrDefault<IfcBuildingElementProxy>();

    //add color to the proper
    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 = "MyStale";
    newStyledItem.Item = element;   //Here something is wrong.
    newStyledItem.Styles.Add(newStyleAssignment);

    using (var txn = model.BeginTransaction(""))
    {

       element.Name = "SomeName";

        txn.Commit();
    }

    model.SaveAs(fileName + "_mod.ifc");

}
martin1cerny commented 4 months ago

It is what it says - invalid assignment. Style should not be assigned to an instance of the building element, but to its geometrical representation items. You need to drill down to element.Representation.Representations.Items to find the items you want to style: IfcStyledItem

andyward commented 4 months ago

Backing up a bit

It's probably worth checking what you're hoping to do here in terms of a use case. This code is amending the styling of a representation of an item in a model. If you save and re-open the IFC file it will should have changed to orange. But it won't change instantly. If you just wanted to highlight the element in real-time this would probably be the wrong approach.

i.e. 2 use cases:

1) the client wants to permanently change all the walls to be orange in the handover model - satisfied by this approach 2) Highlight in orange all elements that meet some external criteria - not satisfied by this approach There are better ways but we would need to know your use case & UI tech choices.

wojtpl commented 4 months ago

The idea is that I have a drawing program, which can generate IFC but I guess no color representation. I want to write c# standalone app to create copy (modyfied) IFC with IFCBuildingElementProxy named i.e. "Beam, Column, Plate" and then assign color to geometry name to make my IFC easier to read. I am static engineer, not professional programmer. But i guess I have to get deeper in IFC structure. If you know any sources where I can improve my skill please send a link and wish me luck :)

martin1cerny commented 3 months ago

So, you want to create a kind-off analytical view of the IFC? When you say it can generate IFC, is that your code, already written using the Toolkit?