Hide menu
Loading...
Searching...
No Matches
exploring/brep_topology/Program.cs

Refer to the B-Rep Topology Exploration Example.

// ****************************************************************************
// $Id$
//
// Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
// Copyright (C) 2014-2025, CADEX. All rights reserved.
//
// This file is part of the Manufacturing Toolkit software.
//
// You may use this file under the terms of the BSD license as follows:
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// ****************************************************************************
using cadex;
using cadex.Collections;
using cadex.Geom;
using System;
using System.Collections.Generic;
namespace brep_topology
{
class Program
{
static int Main(string[] args)
{
string aKey = MTKLicenseKey.Value();
// Activate the license (aKey must be defined in mtk_license.cs)
if (!LicenseManager.Activate(aKey))
{
Console.WriteLine("Failed to activate Manufacturing Toolkit license.");
return 1;
}
if (args.Length != 1)
{
Console.WriteLine("Usage: " +
$"{System.Reflection.Assembly.GetExecutingAssembly().Location} <input_file>, where:");
Console.WriteLine($" <input_file> is a name of the file to be read");
Console.WriteLine($"");
return 1;
}
string aSource = args[0];
Model aModel = new Model();
if (!new ModelReader().Read(new UTF16String(aSource), aModel))
{
Console.WriteLine("Failed to read the file " + aSource);
return 1;
}
// Explore B-Rep representation of model parts
PartBRepVisitor aVisitor = new PartBRepVisitor();
aModel.Accept(aVisitor);
aVisitor.PrintUniqueShapesCount();
return 0;
}
}
class UnorintedShapeHasher : IEqualityComparer<Shape>
{
public bool Equals(Shape theFirstShape, Shape theSecondShape)
{
return theFirstShape.IsSame(theSecondShape);
}
public int GetHashCode(Shape theShape)
{
return (int)theShape.Id();
}
}
// Visits directly each part and calls B-Rep bodies exploring if a part has one
class PartBRepVisitor : ModelElementVoidVisitor
{
public PartBRepVisitor()
{
myNestingLevel = 0;
}
public void PrintUniqueShapesCount()
{
Console.WriteLine();
Console.WriteLine("Total unique shapes count: " + myShapeSet.Count);
}
public override void Apply(Part thePart)
{
Console.WriteLine("Part = \"" + thePart.Name() + "\"" );
var aBodies = thePart.Bodies();
if (!aBodies.IsEmpty)
{
ExploreBRep(aBodies);
}
}
private void ExploreBRep(BodyList theBodies)
{
myNestingLevel++;
int i = 0;
// Iterate over bodies
foreach (var aBody in theBodies)
{
PrintTabulation();
Console.WriteLine("Body " + i + ": -type " + PrintBodyType(aBody));
++i;
ShapeIterator aFaceIt = new ShapeIterator(aBody);
for (var j = new ShapeIterator (aBody); j.HasNext();)
{
var aShape = j.Next();
ExploreShape(aShape);
}
}
myNestingLevel--;
}
// Recursive iterating over the Shape until reaching vertices
private void ExploreShape(Shape theShape)
{
myShapeSet.Add(theShape);
++myNestingLevel;
ShapeIterator aShapeIt = new ShapeIterator(theShape);
while (aShapeIt.HasNext())
{
Shape aShape = aShapeIt.Next();
PrintShapeInfo(aShape);
ExploreShape(aShape);
}
--myNestingLevel;
}
// Returns body type name
private string PrintBodyType(Body theBody)
{
if (SheetBody.CompareType(theBody))
{
return "Sheet";
}
else if (SolidBody.CompareType(theBody))
{
return "Solid";
}
else if (WireframeBody.CompareType(theBody))
{
return "Wireframe";
}
return "Undefined";
}
// Prints shape type name and prints shape info in some cases
private void PrintShapeInfo(Shape theShape)
{
PrintTabulation();
switch (theShape.Type())
{
case ShapeType.Solid: Console.Write("Solid"); break;
case ShapeType.Shell: Console.Write("Shell"); break;
case ShapeType.Wire:
Console.Write("Wire");
PrintWireInfo(Wire.Cast(theShape));
break;
case ShapeType.Face:
Console.Write("Face");
PrintFaceInfo(Face.Cast(theShape));
break;
case ShapeType.Edge:
Console.Write("Edge");
PrintEdgeInfo(Edge.Cast(theShape));
break;
case ShapeType.Vertex:
Console.Write("Vertex");
PrintVertexInfo(Vertex.Cast(theShape));
break;
default: Console.Write("Undefined"); break;
}
Console.WriteLine();
}
private void PrintOrientationInfo(Shape theShape)
{
Console.Write(". Orientation: ");
switch (theShape.Orientation())
{
case ShapeOrientation.Forward: Console.Write("Forward"); break;
case ShapeOrientation.Reversed: Console.Write("Reversed"); break;
default:
break;
}
}
private void PrintWireInfo(Wire theWire)
{
++myNestingLevel;
PrintOrientationInfo(theWire);
--myNestingLevel;
}
public void PrintFaceInfo(Face theFace)
{
++myNestingLevel;
PrintOrientationInfo(theFace);
Console.WriteLine();
Surface aSurface = theFace.Surface();
PrintTabulation();
Console.Write("Surface: " + PrintSurfaceType(aSurface));
--myNestingLevel;
}
private string PrintSurfaceType(Surface theSurface)
{
switch (theSurface.Type())
{
case SurfaceType.Plane: return "Plane";
case SurfaceType.Cylinder: return "Cylinder";
case SurfaceType.Cone: return "Cone";
case SurfaceType.Sphere: return "Sphere";
case SurfaceType.Torus: return "Torus";
case SurfaceType.LinearExtrusion: return "LinearExtrusion";
case SurfaceType.Revolution: return "Revolution";
case SurfaceType.Bezier: return "Bezier";
case SurfaceType.BSpline: return "BSpline";
case SurfaceType.Offset: return "Offset";
default:
break;
}
return "Undefined";
}
private void PrintEdgeInfo(Edge theEdge)
{
++myNestingLevel;
if (theEdge.IsDegenerated())
{
Console.Write("(Degenerated)");
}
PrintOrientationInfo(theEdge);
Console.Write(". Tolerance " + theEdge.Tolerance());
if (!theEdge.IsDegenerated())
{
Console.WriteLine();
double aParamFirst = 0.0, aParamLast = 0.0;
Curve aCurve = theEdge.Curve(out aParamFirst, out aParamLast);
PrintTabulation();
Console.Write("Curve: " + PrintCurveType(aCurve));
}
--myNestingLevel;
}
private string PrintCurveType(Curve theCurve)
{
switch (theCurve.Type())
{
case CurveType.Line: return "Line";
case CurveType.Circle: return "Circle";
case CurveType.Ellipse: return "Ellipse";
case CurveType.Hyperbola: return "Hyperbola";
case CurveType.Parabola: return "Parabola";
case CurveType.Bezier: return "Bezier";
case CurveType.BSpline: return "BSpline";
case CurveType.Offset: return "Offset";
default:
break;
}
return "Undefined";
}
private void PrintVertexInfo(Vertex theVertex)
{
PrintOrientationInfo(theVertex);
Console.Write(". Tolerance " + theVertex.Tolerance());
}
private void PrintTabulation()
{
for (int i = 0; i < myNestingLevel; ++i)
{
Console.Write("- ");
}
}
// Set to collect unique shapes from model
private HashSet<Shape> myShapeSet = new HashSet<Shape>(new UnorintedShapeHasher());
private int myNestingLevel;
}
}
size_t Id() const
Return unique identifier of public object.
Definition BaseObject.cxx:50
Base class for 3D curves.
Definition Curve.hxx:39
CurveType Type() const
Definition Curve.cxx:434
Base class for geometrical surfaces.
Definition Surface.hxx:40
SurfaceType Type() const
Returns a surface type.
Definition Surface.cxx:495
Provides a base body class.
Definition Body.hxx:30
Defines an edge.
Definition Edge.hxx:33
Geom::Curve Curve(double &theFirstParameter, double &theLastParameter) const
Returns edge 3D curve and its limits.
Definition Edge.cxx:408
double Tolerance() const
Returns edge tolerance.
Definition Edge.cxx:682
bool IsDegenerated() const
Returns true if the edge is degenerated.
Definition Edge.cxx:689
static const Edge & Cast(const Shape &theShape)
Casts a base class object to Edge.
Definition Edge.cxx:714
Defines a topological face.
Definition Face.hxx:33
Geom::Surface Surface() const
Returns underlying surface.
Definition Face.cxx:324
static const Face & Cast(const Shape &theShape)
Cast operator.
Definition Face.cxx:355
UTF16String Name() const
Returns a name.
Definition ModelElement.cxx:55
Element visitor with empty implementation.
Definition ModelElementVisitor.hxx:64
Provides MTK data model.
Definition Model.hxx:40
void Accept(ModelElementVisitor &theVisitor) const
Accepts a visitor.
Definition Model.cxx:274
Reads STEP and native format.
Definition ModelReader.hxx:33
Defines a leaf node in the scene graph hiearchy.
Definition Part.hxx:34
Base class of topological shapes.
Definition Shape.hxx:38
bool IsSame(const Shape &theOther) const
Returns true if the shape shares the same geometry and subshape graph.
Definition Shape.cxx:322
ShapeOrientation Orientation() const
Returns orientation flag.
Definition Shape.cxx:271
ShapeType Type() const
Returns a shape type. For a null object returns Undefined.
Definition Shape.cxx:259
Iterates over subshapes in a shape.
Definition ShapeIterator.hxx:32
Provides a sheet body composed of faces and shells.
Definition SheetBody.hxx:34
Provides a solid body composed of solids.
Definition SolidBody.hxx:30
Defines topological vertex.
Definition Vertex.hxx:30
double Tolerance() const
Returns vertex tolerance.
Definition Vertex.cxx:117
Defines a connected set of edges.
Definition Wire.hxx:32
Provides a wireframe body composed of edges and wires.
Definition WireframeBody.hxx:34
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition UTF16String.hxx:30
Contains classes, types, enums, and functions related to geometric entities.
SurfaceType
Defines surface type.
Definition SurfaceType.hxx:27
CurveType
Defines curve type.
Definition CurveType.hxx:27
Defines classes, types, enums, and functions related to topological entities and scene graph elements...
ShapeType
Defines shape type.
Definition ShapeType.hxx:27
ShapeOrientation
Defines shape orientation.
Definition ShapeOrientation.hxx:27
Contains classes, namespaces, enums, types, and global functions related to Manufacturing Toolkit.
Definition LicenseManager_LicenseError.hxx:30