Hide menu
Loading...
Searching...
No Matches
meshing/visualizationmesher/Program.cs

Refer to the Visualization Mesher 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 visualizationmesher
{
class FirstFaceGetter : ModelData_Model.VoidElementVisitor
{
public override void Apply(ModelData_Part thePart)
{
if (myFace == null) {
ExploreBRep(thePart.BRepRepresentation());
}
}
public ModelData_Face FirstFace()
{
return myFace;
}
private void ExploreBRep(ModelData_BRepRepresentation theBRep)
{
ModelData_BodyList aBodyList = theBRep.Get();
ModelData_Body aFirstBody = aBodyList.First();
new ModelData_Shape.Iterator(aFirstBody, ModelData_ShapeType.ModelData_ST_Face);
ModelData_Shape aFirstShape = aFaceIt.Next();
ModelData_Face aFirstFace = ModelData_Face.Cast(aFirstShape);
myFace = aFirstFace;
}
private ModelData_Face myFace = null;
};
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 void PrintFaceToPolyAssociation(ModelData_Face theFace, ModelData_BRepToPolyAssociations theAssociations)
{
ModelData_MeshPatch aMeshPatch = theAssociations.Get(theFace);
ModelData_IndexArray aTriangleIndices = aMeshPatch.Indices();
Console.WriteLine("Face triangulation contains {0} triangles", aTriangleIndices.Size());
uint aNumberOfTrianglesToPrint = Math.Min(4, aTriangleIndices.Size());
for (uint i = 0; i < aNumberOfTrianglesToPrint; ++i) {
int aTriangleIndex = aTriangleIndices.Element(i);
Console.WriteLine("Triangle index {0} with vertices: ", aTriangleIndex);
for (int aVertexNumber = 0; aVertexNumber < 3; ++aVertexNumber) {
int aVertexIndex = anITS.CoordinateIndex(aTriangleIndex, aVertexNumber);
ModelData_Point aPoint = anITS.Coordinate(aVertexIndex);
Console.WriteLine(" Vertex index {0} with coords (X: {1}, Y: {2}, Z: {3})", aVertexIndex,
aPoint.X(), aPoint.Y(), aPoint.Z());
}
}
}
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 (!aReader.Read(new Base_UTF16String(aSource), aModel))
{
Console.WriteLine("Failed to read the file " + aSource);
return 1;
}
// Set up mesher and parameters
aParam.SetAngularDeflection(Math.PI * 10 / 180);
aParam.SetChordalDeflection(0.003);
aMesher.Compute(aModel, true);
// Example of using B-Rep to Poly representation associations:
FirstFaceGetter aVisitor = new FirstFaceGetter();
aModel.Accept(aVisitor);
ModelData_Face aFace = aVisitor.FirstFace();
ModelData_BRepToPolyAssociations aBRepToPolyAssociations = aMesher.BRepToPolyAssociations();
PrintFaceToPolyAssociation(aFace, aBRepToPolyAssociations);
// Save the result
if (!aWriter.Write(aModel, new Base_UTF16String("VisMesher.cdx")))
{
Console.WriteLine("Unable to save the model");
return 1;
}
return 0;
}
}
}
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
const ModelData_BRepToPolyAssociations & BRepToPolyAssociations() const
Returns ModelData_BRepToPolyAssociations object which contain associations between shapes and meshes.
Definition: ModelAlgo_BRepMesher.cxx:679
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
void SetAngularDeflection(double theValue)
Sets angular deflection.
Definition: ModelAlgo_BRepMesherParameters.cxx:285
void SetSaveBRepToPolyAssociations(bool theValue)
Sets whether associations between ModelData_Shapes and meshes should be generated.
Definition: ModelAlgo_BRepMesherParameters.cxx:480
void SetChordalDeflection(double theValue)
Sets chordal deflection.
Definition: ModelAlgo_BRepMesherParameters.cxx:191
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
Contains connecting information between B-Rep and Poly representation.
Definition: ModelData_BRepToPolyAssociations.hxx:42
ModelData_MeshPatch Get(const ModelData_Vertex &theVertex) const
Returns the result of meshing the given vertex.
Definition: ModelData_BRepToPolyAssociations.cxx:209
Defines a root topological shape that can be owned by B-Rep representation.
Definition: ModelData_Body.hxx:28
Defines a list of bodies.
Definition: ModelData_BodyList.hxx:31
Defines a topological face.
Definition: ModelData_Face.hxx:32
static const ModelData_Face & Cast(const ModelData_Shape &theShape)
Cast operator.
Definition: ModelData_Face.cxx:282
Defines an array of indices.
Definition: ModelData_IndexArray.hxx:15
IndexType Element(size_t theIndex) const
Returns the requested index.
Definition: ModelData_IndexArray.cxx:48
size_t Size() const
Returns the size of array.
Definition: ModelData_IndexArray.cxx:41
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
IndexType CoordinateIndex(IndexType theFace, IndexType theVertexSlot) const
Returns a coordinate index for a vertex in a face.
Definition: ModelData_IndexedTriangleSet.cxx:218
Represents piece of ModelData_PolyVertexSet using set of indices.
Definition: ModelData_MeshPatch.hxx:36
const ModelData_IndexArray & Indices() const
Returns IndexArray representing this patch.
Definition: ModelData_MeshPatch.cxx:70
const ModelData_PolyVertexSet & PVS() const
Returns full mesh (ModelData_PolyVertexSet).
Definition: ModelData_MeshPatch.cxx:65
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
Writes any format that CAD Exchanger can export.
Definition: ModelData_ModelWriter.hxx:33
bool Write(const ModelData_Model &theModel, const Base_UTF16String &theFilePath)
Writes the specified model to the file at the specified path.
Definition: ModelData_ModelWriter.cxx:143
Defines a leaf node in the scene graph hiearchy.
Definition: ModelData_Part.hxx:35
ModelData_BRepRepresentation BRepRepresentation() const
Definition: ModelData_Part.cxx:360
Defines a 3D point.
Definition: ModelData_Point.hxx:295
Iterates over subshapes in a shape.
Definition: ModelData_Shape.hxx:41
Base class of topological shapes.
Definition: ModelData_Shape.hxx:37
Defines classes, types, and global functions related to CAD Exchanger.
Definition: A3DSTestLib.hxx:22
ModelData_ShapeType
Defines shape type.
Definition: ModelData_ShapeType.hxx:25