How to export/write Coordinate System in STEP file

Mosharraf

Looking around for some CAD
Hi,
Is there any example or code snip on how to export/write coordinate system in STEP file.
I am able to write Solid, Curves, Points but didn't find a way to write coordinate system in STEP file.
Thanks in advance.
 

Andrey_mv

Looking around for some CAD
You can try using GeomToStep_MakeAxis2Placement3d.

Example:

__________________________________________________________________________________________
coordinate system - CS

GeomToStep_MakeAxis2Placement3d geomStepMaker(CS);
Handle(StepGeom_Axis2Placement3d) stepGeomTo3D= geomStepMaker.Value();
stepGeomTo3D->SetName(new TCollection_HAsciiString(NAME);

Handle(StepRepr_HArray1OfRepresentationItem) CSs = new StepRepr_HArray1OfRepresentationItem(1, SIZE_OF_CSs);
CSs->SetValue(i, stepGeomTo3D);

Handle(StepRepr_Representation) repr = ....
Handle(StepRepr_ConstructiveGeometryRepresentation) CGR = new StepRepr_ConstructiveGeometryRepresentation();
CGR->Init(new TCollection_HAsciiString("..."), CSs, repr->ContextOfItems());

Handle(StepRepr_ConstructiveGeometryRepresentationRelationship) CGRR = new StepRepr_ConstructiveGeometryRepresentationRelationship();

CGRR->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), repr, CGR);

std::shared_ptr<STEPControl_Writer> writer = new ....
Handle(XSControl_WorkSession) ws = writer->WS();
Handle(StepData_StepModel) model = Handle(StepData_StepModel):: DownCast(ws->Model());
model->AddWithRefs(CGRR);
________________________________________________________________________________________

This is some pseudo example.
 
Last edited:

Andrey

Active CAD practitioner
Staff member
As I wrote earlier, this is pseudo-code, so you need to get the "Handle(StepRepr_Representation) repr" and the rest of the missing parts. If it is necessary, then I will write completely.
 

Mosharraf

Looking around for some CAD
As I wrote earlier, this is pseudo-code, so you need to get the "Handle(StepRepr_Representation) repr" and the rest of the missing parts. If it is necessary, then I will write completely.
Thanks Andrey, Please write the complete code, it will be very helpful.
 

Andrey

Active CAD practitioner
Staff member
There is no way to write CS directly. As an option, you can bind CS to shape itself, or try to write CS to any other entity. I will give the first option as an example (it is easier to understand). The second example is more "dangerous" - I'm afraid we might go beyond the ISO of STEP.

You can try to write CS as shape, but I'm afraid that this may also conflict with ISO. If you don't want to mess around with this, then create an empty shape and bind CS to it.

Code:
#include <memory>
#include <STEPControl_Writer.hxx>
#include <XSControl_WorkSession.hxx>
#include <StepData_StepModel.hxx>
#include <GeomToStep_MakeAxis2Placement3d.hxx>
#include <StepGeom_Axis2Placement3d.hxx>
#include <StepRepr_HArray1OfRepresentationItem.hxx>
#include <StepRepr_Representation.hxx>
#include <StepRepr_ConstructiveGeometryRepresentation.hxx>
#include <StepRepr_ConstructiveGeometryRepresentationRelationship.hxx>
#include <Interface_Graph.hxx>
#include <StepBasic_Product.hxx>
#include <StepBasic_ProductDefinitionFormation.hxx>
#include <Interface_EntityIterator.hxx>
#include <StepBasic_ProductDefinitionFormation.hxx>
#include <StepBasic_ProductDefinition.hxx>
#include <StepRepr_ProductDefinitionShape.hxx>
#include <StepShape_ShapeDefinitionRepresentation.hxx>
#include <TCollection_HAsciiString.hxx>
#include <TopoDS_Shape.hxx>
#include <gp_Ax3.hxx>
#include <gp_Pnt.hxx>
#include <gp_Dir.hxx>
#include <BRepBuilderAPI_MakeVertex.hxx>

int main()
{
  gp_Ax3 CS;
  CS.SetLocation(gp_Pnt(-10.0, -10.0, 10.0));
  CS.SetDirection(gp_Dir(1.0, 1.0, 1.0));

  TopoDS_Shape vertex = BRepBuilderAPI_MakeVertex(gp_Pnt(100.0, 100.0, 100.0));

  std::shared_ptr<STEPControl_Writer> writer = std::make_shared<STEPControl_Writer>();

  writer->Transfer(vertex, STEPControl_AsIs);

  Handle(XSControl_WorkSession) ws = writer->WS();
  Handle(StepData_StepModel) model = Handle(StepData_StepModel)::DownCast(ws->Model());
  Interface_Graph graph = ws->Graph();

  // Find product
  for (int index = 1; index <= model->NbEntities(); index++)
  {
    Handle(Standard_Transient) entity = model->Entity(index);
    if (!entity->IsKind(STANDARD_TYPE(StepBasic_Product)))
    {
      continue;
    }
    Handle(StepBasic_Product) product = Handle(StepBasic_Product)::DownCast(entity);

    // Find shape representation.
    Handle(StepBasic_ProductDefinitionFormation) productDefinitionFormation = nullptr;
    Interface_EntityIterator itPDF = graph.Sharings(product);
    for (; itPDF.More(); itPDF.Next())
    {
      productDefinitionFormation = Handle(StepBasic_ProductDefinitionFormation)::DownCast(itPDF.Value());
      if (!productDefinitionFormation.IsNull())
      {
        break;
      }
    }
    if (productDefinitionFormation.IsNull())
    {
      continue;
    }

    Handle(StepBasic_ProductDefinition) productDefinition = nullptr;
    Interface_EntityIterator itPD = graph.Sharings(productDefinitionFormation);
    for (; itPD.More(); itPD.Next())
    {
      productDefinition = Handle(StepBasic_ProductDefinition)::DownCast(itPD.Value());
      if (!productDefinition.IsNull())
      {
        break;
      }
    }
    if (productDefinition.IsNull())
    {
      continue;
    }

    Handle(StepRepr_ProductDefinitionShape) productDefinitionShape = nullptr;
    Interface_EntityIterator itPDS = graph.Sharings(productDefinition);
    for (; itPDS.More(); itPDS.Next())
    {
      productDefinitionShape = Handle(StepRepr_ProductDefinitionShape)::DownCast(itPDS.Value());
      if (!productDefinitionShape.IsNull())
      {
        break;
      }
    }
    if (productDefinitionShape.IsNull())
    {
      continue;
    }

    Handle(StepShape_ShapeDefinitionRepresentation) shapeDefinitionRepresentation = nullptr;
    Interface_EntityIterator itSDR = graph.Sharings(productDefinitionShape);
    for (; itSDR.More(); itSDR.Next())
    {
      shapeDefinitionRepresentation = Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(itSDR.Value());
      if (!shapeDefinitionRepresentation.IsNull())
      {
        break;
      }
    }
    if (shapeDefinitionRepresentation.IsNull())
    {
      continue;
    }

    Handle(StepRepr_Representation) repr = shapeDefinitionRepresentation->UsedRepresentation();

    GeomToStep_MakeAxis2Placement3d geomStepMaker(CS);
    Handle(StepGeom_Axis2Placement3d) stepGeomTo3D = geomStepMaker.Value();
    stepGeomTo3D->SetName(new TCollection_HAsciiString("OUR CS"));

    Handle(StepRepr_HArray1OfRepresentationItem) CSs = new StepRepr_HArray1OfRepresentationItem(1, 1);
    CSs->SetValue(1, stepGeomTo3D);

    Handle(StepRepr_ConstructiveGeometryRepresentation) CGR = new StepRepr_ConstructiveGeometryRepresentation();
    CGR->Init(new TCollection_HAsciiString("additional coordinate system"), CSs, repr->ContextOfItems());

    Handle(StepRepr_ConstructiveGeometryRepresentationRelationship) CGRR = new StepRepr_ConstructiveGeometryRepresentationRelationship();

    CGRR->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), repr, CGR);

    model->AddWithRefs(CGRR);
  }

  std::string path = "C:/.../shape_withCS.stp";
  writer->Write(path.c_str());
  return 0;
}

In the example above, a vertex is created and a coordinate system is attached to it.

1652683717560.png

1) First, we find the product (#7).
2) Further, going through the "links"/entities, we find where the shape is (#3->#10).
3) "Connect" the Shape and CS (#23).
 

Andrey

Active CAD practitioner
Staff member
Regarding
You can try to write CS as shape, but I'm afraid that this may also conflict with ISO.

Instead of model->AddWithRefs(CGRR); you can try something like model->AddEntity(CGR);

And accordingly, do not forget to find where to insert this code.
 

Mosharraf

Looking around for some CAD
There is no way to write CS directly. As an option, you can bind CS to shape itself, or try to write CS to any other entity. I will give the first option as an example (it is easier to understand). The second example is more "dangerous" - I'm afraid we might go beyond the ISO of STEP.

You can try to write CS as shape, but I'm afraid that this may also conflict with ISO. If you don't want to mess around with this, then create an empty shape and bind CS to it.

Code:
#include <memory>
#include <STEPControl_Writer.hxx>
#include <XSControl_WorkSession.hxx>
#include <StepData_StepModel.hxx>
#include <GeomToStep_MakeAxis2Placement3d.hxx>
#include <StepGeom_Axis2Placement3d.hxx>
#include <StepRepr_HArray1OfRepresentationItem.hxx>
#include <StepRepr_Representation.hxx>
#include <StepRepr_ConstructiveGeometryRepresentation.hxx>
#include <StepRepr_ConstructiveGeometryRepresentationRelationship.hxx>
#include <Interface_Graph.hxx>
#include <StepBasic_Product.hxx>
#include <StepBasic_ProductDefinitionFormation.hxx>
#include <Interface_EntityIterator.hxx>
#include <StepBasic_ProductDefinitionFormation.hxx>
#include <StepBasic_ProductDefinition.hxx>
#include <StepRepr_ProductDefinitionShape.hxx>
#include <StepShape_ShapeDefinitionRepresentation.hxx>
#include <TCollection_HAsciiString.hxx>
#include <TopoDS_Shape.hxx>
#include <gp_Ax3.hxx>
#include <gp_Pnt.hxx>
#include <gp_Dir.hxx>
#include <BRepBuilderAPI_MakeVertex.hxx>

int main()
{
  gp_Ax3 CS;
  CS.SetLocation(gp_Pnt(-10.0, -10.0, 10.0));
  CS.SetDirection(gp_Dir(1.0, 1.0, 1.0));

  TopoDS_Shape vertex = BRepBuilderAPI_MakeVertex(gp_Pnt(100.0, 100.0, 100.0));

  std::shared_ptr<STEPControl_Writer> writer = std::make_shared<STEPControl_Writer>();

  writer->Transfer(vertex, STEPControl_AsIs);

  Handle(XSControl_WorkSession) ws = writer->WS();
  Handle(StepData_StepModel) model = Handle(StepData_StepModel)::DownCast(ws->Model());
  Interface_Graph graph = ws->Graph();

  // Find product
  for (int index = 1; index <= model->NbEntities(); index++)
  {
    Handle(Standard_Transient) entity = model->Entity(index);
    if (!entity->IsKind(STANDARD_TYPE(StepBasic_Product)))
    {
      continue;
    }
    Handle(StepBasic_Product) product = Handle(StepBasic_Product)::DownCast(entity);

    // Find shape representation.
    Handle(StepBasic_ProductDefinitionFormation) productDefinitionFormation = nullptr;
    Interface_EntityIterator itPDF = graph.Sharings(product);
    for (; itPDF.More(); itPDF.Next())
    {
      productDefinitionFormation = Handle(StepBasic_ProductDefinitionFormation)::DownCast(itPDF.Value());
      if (!productDefinitionFormation.IsNull())
      {
        break;
      }
    }
    if (productDefinitionFormation.IsNull())
    {
      continue;
    }

    Handle(StepBasic_ProductDefinition) productDefinition = nullptr;
    Interface_EntityIterator itPD = graph.Sharings(productDefinitionFormation);
    for (; itPD.More(); itPD.Next())
    {
      productDefinition = Handle(StepBasic_ProductDefinition)::DownCast(itPD.Value());
      if (!productDefinition.IsNull())
      {
        break;
      }
    }
    if (productDefinition.IsNull())
    {
      continue;
    }

    Handle(StepRepr_ProductDefinitionShape) productDefinitionShape = nullptr;
    Interface_EntityIterator itPDS = graph.Sharings(productDefinition);
    for (; itPDS.More(); itPDS.Next())
    {
      productDefinitionShape = Handle(StepRepr_ProductDefinitionShape)::DownCast(itPDS.Value());
      if (!productDefinitionShape.IsNull())
      {
        break;
      }
    }
    if (productDefinitionShape.IsNull())
    {
      continue;
    }

    Handle(StepShape_ShapeDefinitionRepresentation) shapeDefinitionRepresentation = nullptr;
    Interface_EntityIterator itSDR = graph.Sharings(productDefinitionShape);
    for (; itSDR.More(); itSDR.Next())
    {
      shapeDefinitionRepresentation = Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(itSDR.Value());
      if (!shapeDefinitionRepresentation.IsNull())
      {
        break;
      }
    }
    if (shapeDefinitionRepresentation.IsNull())
    {
      continue;
    }

    Handle(StepRepr_Representation) repr = shapeDefinitionRepresentation->UsedRepresentation();

    GeomToStep_MakeAxis2Placement3d geomStepMaker(CS);
    Handle(StepGeom_Axis2Placement3d) stepGeomTo3D = geomStepMaker.Value();
    stepGeomTo3D->SetName(new TCollection_HAsciiString("OUR CS"));

    Handle(StepRepr_HArray1OfRepresentationItem) CSs = new StepRepr_HArray1OfRepresentationItem(1, 1);
    CSs->SetValue(1, stepGeomTo3D);

    Handle(StepRepr_ConstructiveGeometryRepresentation) CGR = new StepRepr_ConstructiveGeometryRepresentation();
    CGR->Init(new TCollection_HAsciiString("additional coordinate system"), CSs, repr->ContextOfItems());

    Handle(StepRepr_ConstructiveGeometryRepresentationRelationship) CGRR = new StepRepr_ConstructiveGeometryRepresentationRelationship();

    CGRR->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), repr, CGR);

    model->AddWithRefs(CGRR);
  }

  std::string path = "C:/.../shape_withCS.stp";
  writer->Write(path.c_str());
  return 0;
}

In the example above, a vertex is created and a coordinate system is attached to it.

View attachment 251

1) First, we find the product (#7).
2) Further, going through the "links"/entities, we find where the shape is (#3->#10).
3) "Connect" the Shape and CS (#23).
Great! Thank you very much for your time and help!
 

DanB

CAD community veteran
There is no way to write CS directly. As an option, you can bind CS to shape itself, or try to write CS to any other entity. I will give the first option as an example (it is easier to understand). The second example is more "dangerous" - I'm afraid we might go beyond the ISO of STEP.

You can try to write CS as shape, but I'm afraid that this may also conflict with ISO. If you don't want to mess around with this, then create an empty shape and bind CS to it.

Code:
#include <memory>
#include <STEPControl_Writer.hxx>
#include <XSControl_WorkSession.hxx>
#include <StepData_StepModel.hxx>
#include <GeomToStep_MakeAxis2Placement3d.hxx>
#include <StepGeom_Axis2Placement3d.hxx>
#include <StepRepr_HArray1OfRepresentationItem.hxx>
#include <StepRepr_Representation.hxx>
#include <StepRepr_ConstructiveGeometryRepresentation.hxx>
#include <StepRepr_ConstructiveGeometryRepresentationRelationship.hxx>
#include <Interface_Graph.hxx>
#include <StepBasic_Product.hxx>
#include <StepBasic_ProductDefinitionFormation.hxx>
#include <Interface_EntityIterator.hxx>
#include <StepBasic_ProductDefinitionFormation.hxx>
#include <StepBasic_ProductDefinition.hxx>
#include <StepRepr_ProductDefinitionShape.hxx>
#include <StepShape_ShapeDefinitionRepresentation.hxx>
#include <TCollection_HAsciiString.hxx>
#include <TopoDS_Shape.hxx>
#include <gp_Ax3.hxx>
#include <gp_Pnt.hxx>
#include <gp_Dir.hxx>
#include <BRepBuilderAPI_MakeVertex.hxx>

int main()
{
  gp_Ax3 CS;
  CS.SetLocation(gp_Pnt(-10.0, -10.0, 10.0));
  CS.SetDirection(gp_Dir(1.0, 1.0, 1.0));

  TopoDS_Shape vertex = BRepBuilderAPI_MakeVertex(gp_Pnt(100.0, 100.0, 100.0));

  std::shared_ptr<STEPControl_Writer> writer = std::make_shared<STEPControl_Writer>();

  writer->Transfer(vertex, STEPControl_AsIs);

  Handle(XSControl_WorkSession) ws = writer->WS();
  Handle(StepData_StepModel) model = Handle(StepData_StepModel)::DownCast(ws->Model());
  Interface_Graph graph = ws->Graph();

  // Find product
  for (int index = 1; index <= model->NbEntities(); index++)
  {
    Handle(Standard_Transient) entity = model->Entity(index);
    if (!entity->IsKind(STANDARD_TYPE(StepBasic_Product)))
    {
      continue;
    }
    Handle(StepBasic_Product) product = Handle(StepBasic_Product)::DownCast(entity);

    // Find shape representation.
    Handle(StepBasic_ProductDefinitionFormation) productDefinitionFormation = nullptr;
    Interface_EntityIterator itPDF = graph.Sharings(product);
    for (; itPDF.More(); itPDF.Next())
    {
      productDefinitionFormation = Handle(StepBasic_ProductDefinitionFormation)::DownCast(itPDF.Value());
      if (!productDefinitionFormation.IsNull())
      {
        break;
      }
    }
    if (productDefinitionFormation.IsNull())
    {
      continue;
    }

    Handle(StepBasic_ProductDefinition) productDefinition = nullptr;
    Interface_EntityIterator itPD = graph.Sharings(productDefinitionFormation);
    for (; itPD.More(); itPD.Next())
    {
      productDefinition = Handle(StepBasic_ProductDefinition)::DownCast(itPD.Value());
      if (!productDefinition.IsNull())
      {
        break;
      }
    }
    if (productDefinition.IsNull())
    {
      continue;
    }

    Handle(StepRepr_ProductDefinitionShape) productDefinitionShape = nullptr;
    Interface_EntityIterator itPDS = graph.Sharings(productDefinition);
    for (; itPDS.More(); itPDS.Next())
    {
      productDefinitionShape = Handle(StepRepr_ProductDefinitionShape)::DownCast(itPDS.Value());
      if (!productDefinitionShape.IsNull())
      {
        break;
      }
    }
    if (productDefinitionShape.IsNull())
    {
      continue;
    }

    Handle(StepShape_ShapeDefinitionRepresentation) shapeDefinitionRepresentation = nullptr;
    Interface_EntityIterator itSDR = graph.Sharings(productDefinitionShape);
    for (; itSDR.More(); itSDR.Next())
    {
      shapeDefinitionRepresentation = Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(itSDR.Value());
      if (!shapeDefinitionRepresentation.IsNull())
      {
        break;
      }
    }
    if (shapeDefinitionRepresentation.IsNull())
    {
      continue;
    }

    Handle(StepRepr_Representation) repr = shapeDefinitionRepresentation->UsedRepresentation();

    GeomToStep_MakeAxis2Placement3d geomStepMaker(CS);
    Handle(StepGeom_Axis2Placement3d) stepGeomTo3D = geomStepMaker.Value();
    stepGeomTo3D->SetName(new TCollection_HAsciiString("OUR CS"));

    Handle(StepRepr_HArray1OfRepresentationItem) CSs = new StepRepr_HArray1OfRepresentationItem(1, 1);
    CSs->SetValue(1, stepGeomTo3D);

    Handle(StepRepr_ConstructiveGeometryRepresentation) CGR = new StepRepr_ConstructiveGeometryRepresentation();
    CGR->Init(new TCollection_HAsciiString("additional coordinate system"), CSs, repr->ContextOfItems());

    Handle(StepRepr_ConstructiveGeometryRepresentationRelationship) CGRR = new StepRepr_ConstructiveGeometryRepresentationRelationship();

    CGRR->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), repr, CGR);

    model->AddWithRefs(CGRR);
  }

  std::string path = "C:/.../shape_withCS.stp";
  writer->Write(path.c_str());
  return 0;
}

In the example above, a vertex is created and a coordinate system is attached to it.

View attachment 251

1) First, we find the product (#7).
2) Further, going through the "links"/entities, we find where the shape is (#3->#10).
3) "Connect" the Shape and CS (#23).
Wow. That is very useful. I've struggled with the problem myself.
 
Top