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;
}
}
Contains classes, types, enums, and functions related to geometric entities.
Defines classes, types, enums, and functions related to topological entities and scene graph elements...
Contains classes, namespaces, enums, types, and global functions related to Manufacturing Toolkit.