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

Refer to the B-Rep Representation Example.

// ****************************************************************************
// $Id$
//
// Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
// Copyright (C) 2014-2023, CADEX. All rights reserved.
//
// This file is part of the CAD Exchanger 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 System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace brepresentation
{
class Program
{
// For more information see https://stackoverflow.com/questions/8836093/how-can-i-specify-a-dllimport-path-at-runtime
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool SetDllDirectory(string lpPathName);
static int Main(string[] args)
{
// Add runtime path to CAD Exchanger libraries (e.g. compiled with Visual Studio 2015)
SetDllDirectory("../../../../../../win64/vc14.1/bin");
string aKey = LicenseKey.Value();
// Activate the license (aKey must be defined in cadex_license.cs)
if (!LicenseManager.Activate(aKey))
{
Console.WriteLine("Failed to activate CAD Exchanger 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 XML file to be read");
return 1;
}
string aSource = args[0];
if (!new ModelData_ModelReader().Read(new Base_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;
}
}
// Visits directly each part and calls B-Rep representation exploring if a part has one
class PartBRepVisitor : ModelData_Model.VoidElementVisitor
{
public PartBRepVisitor()
{
myNestingLevel = 0;
}
public void PrintUniqueShapesCount()
{
Console.WriteLine();
Console.WriteLine("Total unique shapes count: " + myShapeSet.Count);
}
public override void Apply(ModelData_Part thePart)
{
if (aBRep != null)
{
ExploreBRep(aBRep);
}
}
private void ExploreBRep(ModelData_BRepRepresentation theBRep)
{
// Get() method retrieves bodies listed in B-Rep representation by calling data providers flushing
// Flushing isn't an elementary process so it can take a significant time (seconds, minutes depending on a model structure)
ModelData_BodyList aBodyList = theBRep.Get();
for (uint i = 0; i < aBodyList.Size(); ++i)
{
ModelData_Body aBody = aBodyList.Access(i);
Console.WriteLine("Body " + i + ": -type " + PrintBodyType(aBody));
ExploreShape(aBody);
}
}
// Recursive iterating over the Shape until reaching vertices
private void ExploreShape(ModelData_Shape theShape)
{
myShapeSet.Add(theShape);
++myNestingLevel;
while (aShapeIt.HasNext())
{
ModelData_Shape aShape = aShapeIt.Next();
PrintShapeInfo(aShape);
ExploreShape(aShape);
}
--myNestingLevel;
}
// Returns body type name
private string PrintBodyType(ModelData_Body theBody)
{
switch (theBody.BodyType())
{
case ModelData_BodyType.ModelData_BT_Solid: return "Solid";
case ModelData_BodyType.ModelData_BT_Sheet: return "Sheet";
case ModelData_BodyType.ModelData_BT_Wireframe: return "Wireframe";
case ModelData_BodyType.ModelData_BT_Acorn: return "Acorn";
}
return "Undefined";
}
// Prints shape type name and prints shape info in some cases
private void PrintShapeInfo(ModelData_Shape theShape)
{
PrintTabulation();
switch (theShape.Type())
{
case ModelData_ShapeType.ModelData_ST_Body: Console.Write("Body"); break;
case ModelData_ShapeType.ModelData_ST_Solid: Console.Write("Solid"); break;
case ModelData_ShapeType.ModelData_ST_Shell: Console.Write("Shell"); break;
case ModelData_ShapeType.ModelData_ST_Wire:
Console.Write("Wire");
PrintWireInfo(ModelData_Wire.Cast(theShape));
break;
case ModelData_ShapeType.ModelData_ST_Face:
Console.Write("Face");
PrintFaceInfo(ModelData_Face.Cast(theShape));
break;
case ModelData_ShapeType.ModelData_ST_Edge:
Console.Write("Edge");
PrintEdgeInfo(ModelData_Edge.Cast(theShape));
break;
case ModelData_ShapeType.ModelData_ST_Vertex:
Console.Write("Vertex");
PrintVertexInfo(ModelData_Vertex.Cast(theShape));
break;
default: Console.Write("Undefined"); break;
}
Console.WriteLine();
}
private void PrintOrientationInfo(ModelData_Shape theShape)
{
Console.Write(". Orientation: ");
switch (theShape.Orientation())
{
case ModelData_ShapeOrientation.ModelData_SO_Forward: Console.Write("Forward"); break;
case ModelData_ShapeOrientation.ModelData_SO_Reversed: Console.Write("Reversed"); break;
default:
break;
}
}
private void PrintWireInfo(ModelData_Wire theWire)
{
++myNestingLevel;
PrintOrientationInfo(theWire);
--myNestingLevel;
}
public void PrintFaceInfo(ModelData_Face theFace)
{
++myNestingLevel;
PrintOrientationInfo(theFace);
Console.WriteLine();
ModelData_Surface aSurface = theFace.Surface();
PrintTabulation();
Console.Write("Surface: " + PrintSurfaceType(aSurface));
--myNestingLevel;
}
private string PrintSurfaceType(ModelData_Surface theSurface)
{
switch (theSurface.Type())
{
case ModelData_SurfaceType.ModelData_ST_Plane: return "Plane";
case ModelData_SurfaceType.ModelData_ST_Cylinder: return "Cylinder";
case ModelData_SurfaceType.ModelData_ST_Cone: return "Cone";
case ModelData_SurfaceType.ModelData_ST_Sphere: return "Sphere";
case ModelData_SurfaceType.ModelData_ST_Torus: return "Torus";
case ModelData_SurfaceType.ModelData_ST_LinearExtrusion: return "LinearExtrusion";
case ModelData_SurfaceType.ModelData_ST_Revolution: return "Revolution";
case ModelData_SurfaceType.ModelData_ST_Bezier: return "Bezier";
case ModelData_SurfaceType.ModelData_ST_BSpline: return "BSpline";
case ModelData_SurfaceType.ModelData_ST_Offset: return "Offset";
case ModelData_SurfaceType.ModelData_ST_Trimmed: return "Trimmed";
default:
break;
}
return "Undefined";
}
private void PrintEdgeInfo(ModelData_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;
ModelData_Curve aCurve = theEdge.Curve(ref aParamFirst, ref aParamLast);
PrintTabulation();
Console.Write("Curve: " + PrintCurveType(aCurve));
}
--myNestingLevel;
}
private string PrintCurveType(ModelData_Curve theCurve)
{
switch (theCurve.Type())
{
case ModelData_CurveType.ModelData_CT_Line: return "Line";
case ModelData_CurveType.ModelData_CT_Circle: return "Circle";
case ModelData_CurveType.ModelData_CT_Ellipse: return "Ellipse";
case ModelData_CurveType.ModelData_CT_Hyperbola: return "Hyperbola";
case ModelData_CurveType.ModelData_CT_Parabola: return "Parabola";
case ModelData_CurveType.ModelData_CT_Bezier: return "Bezier";
case ModelData_CurveType.ModelData_CT_BSpline: return "BSpline";
case ModelData_CurveType.ModelData_CT_Offset: return "Offset";
case ModelData_CurveType.ModelData_CT_Trimmed: return "Trimmed";
default:
break;
}
return "Undefined";
}
private void PrintVertexInfo(ModelData_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<ModelData_Shape> myShapeSet = new HashSet<ModelData_Shape>(new ModelData_UnorientedShapeHash());
private int myNestingLevel;
}
}
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition: Base_UTF16String.hxx:34
Defines precise Boundary Representation of part.
Definition: ModelData_BRepRepresentation.hxx:39
const ModelData_BodyList & Get() const
Returns an associated topological object.
Definition: ModelData_BRepRepresentation.cxx:626
Defines a root topological shape that can be owned by B-Rep representation.
Definition: ModelData_Body.hxx:28
ModelData_BodyType BodyType() const
Returns a body type.
Definition: ModelData_Body.cxx:139
Defines a list of bodies.
Definition: ModelData_BodyList.hxx:31
Base class for 3D curves.
Definition: ModelData_Curve.hxx:44
ModelData_CurveType Type() const
Returns a curve type.
Definition: ModelData_Curve.cxx:205
Defines an edge.
Definition: ModelData_Edge.hxx:36
static const ModelData_Edge & Cast(const ModelData_Shape &theShape)
Casts a base class object to ModelData_Edge.
Definition: ModelData_Edge.cxx:688
bool IsDegenerated() const
Returns true if the edge is degenerated.
Definition: ModelData_Edge.cxx:671
double Tolerance() const
Returns edge tolerance.
Definition: ModelData_Edge.cxx:664
ModelData_Curve Curve(double &theFirstParameter, double &theLastParameter) const
Returns edge 3D curve and its limits.
Definition: ModelData_Edge.cxx:632
Defines a topological face.
Definition: ModelData_Face.hxx:32
ModelData_Surface Surface() const
Returns underlying surface.
Definition: ModelData_Face.cxx:253
static const ModelData_Face & Cast(const ModelData_Shape &theShape)
Cast operator.
Definition: ModelData_Face.cxx:282
Provides CAD Exchanger data model.
Definition: ModelData_Model.hxx:43
void Accept(ElementVisitor &theVisitor) const
Accepts a visitor.
Definition: ModelData_Model.cxx:882
Reads any format that CAD Exchanger can import.
Definition: ModelData_ModelReader.hxx:33
Defines a leaf node in the scene graph hiearchy.
Definition: ModelData_Part.hxx:35
ModelData_BRepRepresentation BRepRepresentation() const
Definition: ModelData_Part.cxx:360
Iterates over subshapes in a shape.
Definition: ModelData_Shape.hxx:41
Base class of topological shapes.
Definition: ModelData_Shape.hxx:37
ModelData_ShapeType Type() const
Returns a shape type.
Definition: ModelData_Shape.cxx:358
ModelData_ShapeOrientation Orientation() const
Returns orientation flag.
Definition: ModelData_Shape.cxx:392
Base class for geometrical surfaces.
Definition: ModelData_Surface.hxx:44
ModelData_SurfaceType Type() const
Returns a surface type.
Definition: ModelData_Surface.cxx:195
Hasher for ModelData_Shape using 'IsSame' relationship.
Definition: ModelData_Shape.hxx:110
Defines topological vertex.
Definition: ModelData_Vertex.hxx:31
double Tolerance() const
Returns vertex tolerance.
Definition: ModelData_Vertex.cxx:89
Defines a connected set of edges.
Definition: ModelData_Wire.hxx:31
Defines classes, types, and global functions related to CAD Exchanger.
Definition: A3DSTestLib.hxx:22
ModelData_CurveType
Defines curve type.
Definition: ModelData_CurveType.hxx:25
ModelData_ShapeOrientation
Defines shape orientation.
Definition: ModelData_ShapeOrientation.hxx:25
ModelData_SurfaceType
Defines surface type.
Definition: ModelData_SurfaceType.hxx:25
ModelData_ShapeType
Defines shape type.
Definition: ModelData_ShapeType.hxx:25
ModelData_BodyType
Defines a body type.
Definition: ModelData_BodyType.hxx:25