Hide menu
Loading...
Searching...
No Matches
wall_thickness/analyzer/Program.cs

Refer to the Wall Thickness Analyzer 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;
namespace analyzer
{
class Program
{
static int Main(string[] args)
{
// Add runtime path to CAD Exchanger libraries
cadex.helpers.LoadLibrarySearchDirectory.SetupDllDirectory();
string aKey = LicenseKey.Value();
string anMTKKey = MTKLicenseKey.Value();
// Activate the license (aKey must be defined in cadex_license.cs
// and anMTKKey should be defined in mtk_license.cs)
if (!LicenseManager.Activate(aKey))
{
Console.WriteLine("Failed to activate CAD Exchanger license.");
return 1;
}
if (!LicenseManager.Activate(anMTKKey))
{
Console.WriteLine("Failed to activate Manufacturing Toolkit license.");
return 1;
}
if (args.Length < 1 || args.Length > 2)
{
Console.WriteLine("Usage: " +
$"{System.Reflection.Assembly.GetExecutingAssembly().Location} <input_file> <input_resolution>, where:");
Console.WriteLine($" <input_file> is a name of the file to be read");
Console.WriteLine($" <input_resolution> is an optional argument that determine accuracy of wall thickness calculation." +
$" The larger the value, the higher the accuracy of the calculations," +
$" but greatly increase computation time and memory usage. Should be at least 100.");
return 1;
}
string aSource = args[0];
uint aResolution = 1000;
if (args.Length == 2)
{
aResolution = uint.Parse(args[1]);
}
if (aResolution < 100)
{
Console.WriteLine($"WARNING: Input resolution \"{aResolution}\" < 100. Will be used default resolution.\n");
aResolution = 1000;
}
var aReader = new ModelData_ModelReader();
// Reading the file
if (!aReader.Read(new Base_UTF16String(aSource), aModel)) {
Console.WriteLine($"Failed to read the file {aSource}");
return 1;
}
Console.WriteLine($"Model: {aModel.Name()}\n");
var aPartProcessor = new PartProcessor();
aPartProcessor.Resolution = aResolution;
var aVisitor = new ModelData_SceneGraphElementUniqueVisitor(aPartProcessor);
aModel.Accept(aVisitor);
return 0;
}
class PartProcessor : ModelData_Model.VoidElementVisitor
{
public PartProcessor()
{
myAnalyzer = new WallThickness_Analyzer();
}
public override void Apply(ModelData_Part thePart)
{
var aPartName = thePart.Name().IsEmpty() ? new Base_UTF16String("noname") : thePart.Name();
var aBRep = thePart.BRepRepresentation();
var aPolyRep = thePart.PolyRepresentation(ModelData_RepresentationMask.ModelData_RM_Poly);
if (aBRep != null)
{
// Looking for a suitable body
ModelData_BodyList aBodyList = aBRep.Get();
for (uint i = 0; i < aBodyList.Size(); ++i)
{
ModelData_Body aBody = aBodyList.Element(i);
var aShapeIt = new ModelData_Shape.Iterator(aBody);
while (aShapeIt.HasNext())
{
var aShape = aShapeIt.Next();
if (aShape.Type() == ModelData_ShapeType.ModelData_ST_Solid)
{
Console.Write($"Part #{myPartIndex} [\"{aPartName}\"] - solid #{i} has:\n");
ProcessSolid(ModelData_Solid.Cast(aShape));
}
}
}
}
else if (aPolyRep != null)
{
var aPolyList = aPolyRep.Get();
for (uint i = 0; i < aPolyList.Size(); ++i)
{
var aPVS = aPolyList.Element(i);
if (aPVS.TypeId() == ModelData_IndexedTriangleSet.GetTypeId())
{
Console.Write($"Part #{myPartIndex} [\"{aPartName}\"] - mesh #{i} has:\n");
ProcessMesh(ModelData_IndexedTriangleSet.Cast(aPVS));
}
}
}
++myPartIndex;
}
public void ProcessSolid(ModelData_Solid theSolid)
{
var aWTData = myAnalyzer.Perform(theSolid, Resolution);
PrintWTData(aWTData);
}
public void ProcessMesh(ModelData_IndexedTriangleSet theMesh)
{
var aWTData = myAnalyzer.Perform (theMesh, Resolution);
PrintWTData(aWTData);
}
void PrintWTData(WallThickness_Data theData)
{
if (!theData.IsEmpty())
{
Console.WriteLine($" Min thickness = {theData.MinThickness()} mm");
Console.WriteLine($" Max thickness = {theData.MaxThickness()} mm");
}
else
{
Console.WriteLine(" Failed to analyze the wall thickness of this entity.");
}
}
public uint Resolution { get; set; } = 1000;
private WallThickness_Analyzer myAnalyzer;
private uint myPartIndex = 0;
}
}
}
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition: Base_UTF16String.hxx:34
bool IsEmpty() const
Returns true if the string is empty.
Definition: Base_UTF16String.cxx:233
Base_UTF16String Name() const
Definition: ModelData_BaseObject.cxx:218
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
const ModelData_Body & Element(SizeType theIndex) const
Definition: ModelData_BodyList.cxx:177
Defines a polygonal shape consisting of triangles.
Definition: ModelData_IndexedTriangleSet.hxx:35
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
ModelData_PolyRepresentation PolyRepresentation(ModelData_RepresentationMask theRepresentationMask) const
Definition: ModelData_Part.cxx:371
Defines a visitor that visits each unique element only once.
Definition: ModelData_SceneGraphElementUniqueVisitor.hxx:33
Iterates over subshapes in a shape.
Definition: ModelData_Shape.hxx:41
Base class of topological shapes.
Definition: ModelData_Shape.hxx:37
Defines a topological solid.
Definition: ModelData_Solid.hxx:31
The wall thickness analyzing tool.
Definition: WallThickness_Analyzer.hxx:42
Contains information about minimum and maximum wall thicknesses.
Definition: WallThickness_Data.hxx:39
bool IsEmpty() const
Returns true if WallThickness_Data is empty.
Definition: WallThickness_Data.cxx:97
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
ModelData_ShapeType
Defines shape type.
Definition: ModelData_ShapeType.hxx:25