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

setting object colour for item exported from autocad #543

Open fpz016 opened 6 months ago

fpz016 commented 6 months ago

I am trying to export colours from existing autocad drawing. I have read everything I can find about setting colours. Previously exported IFC files have working colour information. I am able to add the colour information but have not found the way to attach the shape definition to the StyledItem. Thank you.

Assemblies and versions affected:

_Xbim.Essentials v4.0.30319

Steps (or code) to reproduce the issue:

A simple set of steps to reproduce the issue. Provide a small sample file is required and/or a small sample of the failing code (or reference a commit / gist)

internal void UpdateColour(string partNo, Color color)
{
    //need to add to Assignments(HasAssignments showing in template model)
    //add to Associations(HasAssociations "")
    using (var txn = model.BeginTransaction(partNo + " colour update"))
    {
        IfcBuildingElement beam = (IfcBuildingElement)GetBeamFromACAD(partNo, model);

        if (beam != null)
        {
            // Create a presentation style assignment
            var presentationStyleAssignment = model.Instances.New<IfcPresentationStyleAssignment>(psa =>
            {
                // Create a surface style
                var surfaceStyle = model.Instances.New<IfcSurfaceStyle>(ss =>
                {
                    ss.Side = IfcSurfaceSide.BOTH; // or .POSITIVE or .NEGATIVE
                    ss.Styles.Add(model.Instances.New<IfcSurfaceStyleShading>(sss =>
                    {
                        // Create a color (you might need actual RGB values)
                        sss.SurfaceColour = model.Instances.New<IfcColourRgb>(c =>
                        {
                            c.Name = "YourColorName";
                            c.Red = (double)color.Red / 255;
                            c.Green = (double)color.Green / 255;
                            c.Blue = (double)color.Blue / 255;
                        });
                    }));
                });

                // Add the surface style to the presentation style assignment
                psa.Styles.Add(surfaceStyle);
            });

            // Create a styled item
            var styledItem = model.Instances.New<IfcStyledItem>(si =>
            {
                // You can also create a specific representation item here if needed
                si.Styles.Add(presentationStyleAssignment);
                si.Name = "YourStyledItemName";

            });

            // Set the product representation for the beam
            var bReps = beam.Representation.Representations;

            var shapeRepresentations = bReps.OfType<IfcShapeRepresentation>();

            // Output information about each IfcShapeRepresentation
            foreach (var shapeRepresentation in shapeRepresentations)
            {
                //var type = shapeRepresentation.GetType();                 
                var shape = shapeRepresentation.Items[0];
                var modelContext = model.Instances.OfType<IfcGeometricRepresentationContext>().FirstOrDefault();

            }
            /*
             * /Create a Definition shape to hold the geometry
                var shape = model.Instances.New<IfcShapeRepresentation>();
                var modelContext = model.Instances.OfType<IfcGeometricRepresentationContext>().FirstOrDefault();
                shape.ContextOfItems = modelContext;
                shape.RepresentationType = "SweptSolid";
                shape.RepresentationIdentifier = "Body";
                shape.Items.Add(body);
             */
            txn.Commit();
            model.SaveAs(fileName);

        }
    }
}

Minimal file to reproduce the issue:

P0042_01_4x0.zip

Expected behavior:

Colour would be applied to all objects referenced
the lines below are from the working file I have been provided
#10=IFCPRESENTATIONSTYLEASSIGNMENT((#11));
#11=IFCSURFACESTYLE($,.BOTH.,(#12));
#12=IFCSURFACESTYLESHADING(#13,$);
#13=IFCCOLOURRGB('ff7aafdf',0.47843137254902,0.686274509803922,0.874509803921569);
#14=IFCSTYLEDITEM(#15,(#10),$);
#15=IFCEXTRUDEDAREASOLID(#16,#19,#9,1075.);

line #14 includes a reference to item #15 which is how I assume the IfcStyledIem is being applied to the IFCEXTRUDEDAREASOLID. Is this the correct method for IFC4? If so is there a command to apply this information?

Actual behavior or exception details:

IfcStyledItems not associated with IFCEXTRUDEDAREASOLID

martin1cerny commented 6 months ago

You are missing the very last bit - to assign the shape to the style

styledItem.Item = shape;