Get origin (gp_XYZ or gp_Point) from shape

Hello everybody,

Currently i have a simple method which creates a boxed shape and adds it to the XCAF Document.
The vector parameter sets the location of the shape. This seems to work nicely.

 Creating the Shape

	bool CreateModel(Vector^ vector)
	{
		// Create Geometry
		BRepPrimAPI_MakeBox make(2, 2, 2);
		TopoDS_Shape shape = make.Shape();

		// Add Transformation
		gp_Trsf transformation;
		transformation.SetTranslation(gp_Vec(vector->X, vector->Y, vector->Z));
				
		BRepBuilderAPI_Transform transform(shape, transformation, Standard_True);
		shape = transform.Shape();
				
		// Add Color
		Handle(XCAFDoc_ColorTool) color = XCAFDoc_DocumentTool::ColorTool(_doc->Main());
		Quantity_Color shapeColor(192 / 255, 192 / 255, 192 / 255, Quantity_TOC_RGB);
		color->SetColor(shape, shapeColor, XCAFDoc_ColorGen);

		// Add Shape
		Handle(XCAFDoc_ShapeTool) assembly = XCAFDoc_DocumentTool::ShapeTool(_doc->Main());
		TDF_Label label = assembly->AddShape(shape);

		// Add Name
		const char* name = shape.TShape()->get_type_name();
		TDataStd_Name::Set(label, name);

		return true;
	}

Getting Shapes

The following method just iterates through all shapes in an assembly to get common infos.
Currently i am reading color and name of a shape. This works excellently. I have some problems to get the correct coordinate of that previously created shape within the assembly.

IEnumerable<StepPart^>^ Session::GetParts()
	{
		List<StepPart^>^ parts = gcnew List<StepPart^>();

		Handle(XCAFDoc_ShapeTool) assembly = XCAFDoc_DocumentTool::ShapeTool(_doc->Main());
		Handle(XCAFDoc_ColorTool) color = XCAFDoc_DocumentTool::ColorTool(_doc->Main());

		TDF_LabelSequence shapes;
		assembly->GetShapes(shapes);

		for (int i = 1; i <= shapes.Length(); ++i) {

			TopoDS_Shape shape = assembly->GetShape(shapes.Value(i));
			if (shape.IsNull()) continue;

			TDF_Label label = assembly->FindShape(shape);
			if (label.IsNull()) continue;

			Quantity_Color shapeColor(0.0f, 0.0f, 0.0f, Quantity_TOC_RGB);
			if (color->GetColor(shape, XCAFDoc_ColorGen, shapeColor)
				|| color->GetColor(shape, XCAFDoc_ColorSurf, shapeColor)
				|| color->GetColor(shape, XCAFDoc_ColorCurv, shapeColor))
			{
			}

			Handle(TDataStd_Name) nameAttribute;
			if (!label.FindAttribute(TDataStd_Name::GetID(), nameAttribute)) continue;

			String^ name = Utilities::ToString(nameAttribute->Get());

			// Here i want to know the position of that given shape
			// to show some debug informations for the user.
			TopLoc_Location location = shape.Location();
			gp_Trsf transformation = location.Transformation();
			gp_XYZ translation = transformation.TranslationPart();

			StepPosition^ position = gcnew StepPosition(translation.X(), translation.Y(), translation.Z());

			StepPart^ part = gcnew StepPart(name, gcnew StepColor(shapeColor.Red() * 255, shapeColor.Green() * 255, shapeColor.Blue() * 255), position);
			parts->Add(part);
		}

		return gcnew List<StepPart^>(parts);
	}

Currently x y z values are always 0.0.

TopLoc_Location location = shape.Location();
gp_Trsf transformation = location.Transformation();
gp_XYZ translation = transformation.TranslationPart(); // always ends up 0.0000

Did i miss some understanding how to use the location of a shape? Can someone direct me into the right way how to obtain the location in the global 3D space after an shape has been added to an assembly?

Best regards and thanks in advance,

Peter

martin.lofgren_141597's picture

I have a similar problem. Using BRepBuilderAPI_Transform move the shape relative to the global coordinate system, but leaves the local origin intact. Is this the expected behavior? If so, how could I move the local origin and position the shape related to that?

lmaero's picture

Hi guys, did you solve this? Facing the same issue.