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

Refer to the Polygonal 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.Runtime.InteropServices;
namespace polyrepresentation
{
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;
}
// Get the input
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];
// Open the model
if (!aReader.Read(new Base_UTF16String(aSource), aModel))
{
Console.WriteLine("Failed to read the file " + aSource);
return 1;
}
// If there is no Poly representation in the model, mesher will compute it
ModelAlgo_BRepMesher aMesher = new ModelAlgo_BRepMesher(aMesherParams);
aMesher.Compute(aModel);
// Explore Poly representation of model parts
PartPolyVisitor aVisitor = new PartPolyVisitor();
aModel.Accept(aVisitor);
return 0;
}
}
// Visits directly every part and calls Poly representation exploring if a part has one
class PartPolyVisitor : ModelData_Model.VoidElementVisitor
{
public override void Apply(ModelData_Part thePart)
{
if (aPolyRep != null)
{
ExplorePoly(aPolyRep);
}
}
// Explores PolyVertexSets of Poly representation
private void ExplorePoly(ModelData_PolyRepresentation thePoly)
{
// Get() method retrieves PolyVertexSets listed in Poly representation by calling data providers flushing
// Because of internal mechanics providers flushing is faster than for B-Rep representations
ModelData_PolyShapeList aList = thePoly.Get();
// Iterate over PolyShape list
for (uint i = 0; i < aList.Size(); ++i)
{
ModelData_PolyVertexSet aPVS = aList.Element(i);
Console.WriteLine("PolyShape " + i + ":");
PrintPVSInfo(aPVS);
}
}
// Function to print a PolyVertexSet info
private void PrintPVSInfo(ModelData_PolyVertexSet thePVS)
{
if (thePVS.TypeId() == ModelData_IndexedTriangleSet.GetTypeId())
{
Console.WriteLine("IndexedTriangleSet type.");
DumpTriangleSet(ITS);
}
else if (thePVS.TypeId() == ModelData_PolyLineSet.GetTypeId())
{
Console.WriteLine("PolyLineSet type.");
DumpPolyLineSet(PLS);
}
else if (thePVS.TypeId() == ModelData_PolyPointSet.GetTypeId())
{
Console.WriteLine("PolyPointSet type.");
DumpPolyPointSet(PPS);
}
else
{
Console.WriteLine("Undefined type");
}
}
private void DumpPolyPointSet(ModelData_PolyPointSet thePS)
{
int n = thePS.NumberOfVertices();
Console.WriteLine("PolyPoint set contains " + n + " vertices");
for (int i = 0; i < n; ++i)
{
Console.WriteLine("Point " + i + ":");
Console.WriteLine(" Node coordinates:");
ModelData_Point aP = thePS.Coordinate(i);
Console.WriteLine(" (" + aP.X() + ", " + aP.Y() + ", " + aP.Z() + ")");
}
}
// Prints number of PolyLines and local coordinates for every vertex of each PolyLine
private void DumpPolyLineSet(ModelData_PolyLineSet thePLS)
{
int n = thePLS.NumberOfPolyLines();
Console.WriteLine("PolyLine set contains " + n + "PolyLines");
for (int i = 0; i < n; ++i)
{
Console.WriteLine("PolyLine" + i + ":");
Console.WriteLine(" Node coordinates:");
for (int j = 0; j < thePLS.NumberOfVertices(i); ++j)
{
ModelData_Point aV = thePLS.Coordinate(i, j);
Console.WriteLine(" (" + aV.X() + ", " + aV.Y() + ", " + aV.Z() + ")");
}
}
}
// Prints number of triangles and local data for each node
// Prints Coords and UV-Coords for each vertex and prints Normals and Colors for each triangle
private void DumpTriangleSet(ModelData_IndexedTriangleSet theTS)
{
int n = theTS.NumberOfFaces();
Console.WriteLine("Triangle set contains " + n + " number of faces");
for (int i = 0; i < n; ++i)
{
Console.WriteLine("Triangle " + i + ":");
for (int j = 0; j < 3; ++j)
{
Console.WriteLine(" Node " + j + ":");
// Coordinates
ModelData_Point aV = theTS.Coordinate(i, j);
Console.WriteLine(" Coordinates: (" + aV.X() + ", " + aV.Y() + ", " + aV.Z() + ")");
// UV-coordinates
if (theTS.HasUVCoordinates())
{
ModelData_Point2d aUV = theTS.UVCoordinate(i, j);
Console.WriteLine(" UV Coordinates: (" + aUV.X() + ", " + aUV.Y() + ")");
}
// Normals
if (theTS.HasNormals())
{
ModelData_Vectorf aN = theTS.VertexNormal(i, j);
Console.WriteLine(" Normal vector: (" + aN.X() + ", " + aN.Y() + ", " + aN.Z() + ")");
}
// Colors
if (theTS.HasColors())
{
ModelData_Color aColor = theTS.VertexColor(i, j);
Console.WriteLine(" Color(RGBA): " + aColor.R() + ", " + aColor.G() + ", " + aColor.B() + ", " + aColor.A() + ")");
}
}
}
}
}
}
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition: Base_UTF16String.hxx:34
Computes a polygonal representation from a B-Rep one.
Definition: ModelAlgo_BRepMesher.hxx:48
void Compute(const ModelData_Model &theModel, bool theEnforceAddition=false) const
Computes polygonal representations for all parts in the model.
Definition: ModelAlgo_BRepMesher.cxx:478
Defines parameters used by the B-Rep mesher.
Definition: ModelAlgo_BRepMesherParameters.hxx:33
Granularity
Defines target accuracy of the mesh to be generated.
Definition: ModelAlgo_BRepMesherParameters.hxx:35
void SetGranularity(Granularity theVal)
Sets mesh granularity policy.
Definition: ModelAlgo_BRepMesherParameters.cxx:153
IdType TypeId() const
Returns an object type id.
Definition: ModelData_BaseObject.cxx:199
Defines an RGBA color (with alpha channel).
Definition: ModelData_Color.hxx:34
float A() const
Returns alpha-channel.
Definition: ModelData_Color.hxx:76
Defines a polygonal shape consisting of triangles.
Definition: ModelData_IndexedTriangleSet.hxx:35
const CoordType & Coordinate(IndexType theFace, IndexType theVertexSlot) const
Returns a vertex coordinate.
Definition: ModelData_IndexedTriangleSet.cxx:210
bool HasNormals() const
Returns true if the triangle set has explicitly defined normals.
Definition: ModelData_IndexedTriangleSet.cxx:228
bool HasUVCoordinates() const
Returns true if the triangle set has UV coordinates associated with vertices.
Definition: ModelData_IndexedTriangleSet.cxx:289
const NormalType & VertexNormal(IndexType theFace, IndexType theVertexSlot) const
Returns a normal at a vertex in a face.
Definition: ModelData_IndexedTriangleSet.cxx:246
const ModelData_Point2d & UVCoordinate(IndexType theFace, IndexType theVertexSlot) const
Returns a UV coordinate associated with a vertex.
Definition: ModelData_IndexedTriangleSet.cxx:299
const ColorType & VertexColor(IndexType theFace, IndexType theVertexSlot) const
Returns a color at a vertex in a face.
Definition: ModelData_IndexedTriangleSet.cxx:271
IndexType NumberOfFaces() const
Returns a number of faces (triangles).
Definition: ModelData_IndexedTriangleSet.cxx:179
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
bool Read(const Base_UTF16String &theFilePath, ModelData_Model &theModel)
Reads the file at the specified path into the specified model.
Definition: ModelData_ModelReader.cxx:182
Defines a leaf node in the scene graph hiearchy.
Definition: ModelData_Part.hxx:35
ModelData_PolyRepresentation PolyRepresentation(ModelData_RepresentationMask theRepresentationMask) const
Definition: ModelData_Part.cxx:371
Defines a 2D point.
Definition: ModelData_Point2d.hxx:193
Defines a 3D point.
Definition: ModelData_Point.hxx:295
Defines a polygonal shape consisting of polylines.
Definition: ModelData_PolyLineSet.hxx:31
const CoordType & Coordinate(IndexType thePolyline, IndexType theVertexSlot) const
Returns a vertex coordinate.
Definition: ModelData_PolyLineSet.cxx:99
IndexType NumberOfPolyLines() const
Returns a number of polylines.
Definition: ModelData_PolyLineSet.cxx:89
IndexType NumberOfVertices(IndexType thePolyline) const
Returns a number of vertices in a polyline.
Definition: ModelData_PolyLineSet.cxx:80
Defines a polygonal shape consisting of individual points.
Definition: ModelData_PolyPointSet.hxx:31
Defines polygonal (faceted or tessellated) representation of part.
Definition: ModelData_PolyRepresentation.hxx:39
const ModelData_PolyShapeList & Get() const
Returns poly shapes comprising the representation.
Definition: ModelData_PolyRepresentation.cxx:408
List of vertex sets contained in polygonal representation.
Definition: ModelData_PolyShapeList.hxx:33
Base class for particular vertex sets - triangle sets, polyline sets, point sets.
Definition: ModelData_PolyVertexSet.hxx:31
const CoordType & Coordinate(IndexType theVertexIndex) const
Returns a vertex coordinate.
Definition: ModelData_PolyVertexSet.cxx:81
Defines classes, types, and global functions related to CAD Exchanger.
Definition: A3DSTestLib.hxx:22
ModelData_RepresentationMask
Defines a mask to filter part representations.
Definition: ModelData_RepresentationMask.hxx:25