Hide menu
Loading...
Searching...
No Matches
modification/renaming/Program.cs

Refer to the Renaming 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 renaming
{
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 != 2)
{
Console.WriteLine("Usage: " + System.Reflection.Assembly.GetExecutingAssembly().Location
+ " <input_file> <output_file>, where:");
Console.WriteLine(" <input_file> is a name of the model file to be read");
Console.WriteLine(" <output_file> is a name of the model file to be written");
return 1;
}
string aSource = args[0];
string aDest = args[1];
// Opening and converting the file
if (!new ModelData_ModelReader().Read(new Base_UTF16String(aSource), aModel))
{
Console.WriteLine("Failed to read the file " + aSource);
return 1;
}
SceneGraphNameModifier aModifier = new SceneGraphNameModifier();
// Renaming the elements
aModel.Accept(aUniqueModifier);
// Converting and writing the model to file
if (!aWriter.Write(aModel, new Base_UTF16String(aDest)))
{
Console.WriteLine("Failed to convert and write the file to specified format", aDest);
return 1;
}
return 0;
}
class SubshapeNameModifier : ModelData_BRepRepresentation.SubshapeVisitor
{
public SubshapeNameModifier(ModelData_BRepRepresentation theBRep)
{
myBRep = theBRep;
}
public override void Apply(ModelData_Shape theShape)
{
String aNewName = "aNewName";
switch (theShape.Type())
{
case ModelData_ShapeType.ModelData_ST_BodyList:
aNewName = "Body List";
break;
case ModelData_ShapeType.ModelData_ST_Body:
aNewName = "Body";
break;
case ModelData_ShapeType.ModelData_ST_Solid:
aNewName = "Solid";
break;
case ModelData_ShapeType.ModelData_ST_Shell:
aNewName = "Shell";
break;
case ModelData_ShapeType.ModelData_ST_Face:
aNewName = "Face";
break;
case ModelData_ShapeType.ModelData_ST_Wire:
aNewName = "Wire";
break;
case ModelData_ShapeType.ModelData_ST_Edge:
aNewName = "Edge";
break;
case ModelData_ShapeType.ModelData_ST_Vertex:
aNewName = "Vertex";
break;
case ModelData_ShapeType.ModelData_ST_Undefined:
aNewName = "Undefined";
break;
}
aNewName += " " + myBRep.ShapeId(theShape).ToString();
myBRep.SetName(theShape, new Base_UTF16String(aNewName));
}
}
class RepresentationNameModifier : ModelData_Part.RepresentationVisitor
{
public override void Apply(ModelData_BRepRepresentation theBRep)
{
SubshapeNameModifier aModifier = new SubshapeNameModifier(theBRep);
theBRep.Accept(aModifier);
ModelData_BodyList aBodyList = aBRep.Get();
for (uint i = 0; i < aBodyList.Size(); ++i)
{
String aNewName = "aNewName";
switch (aBodyList.Element(i).BodyType())
{
case ModelData_BodyType.ModelData_BT_Acorn:
aNewName = "Acorn Body";
break;
case ModelData_BodyType.ModelData_BT_Wireframe:
aNewName = "Wireframe Body";
break;
case ModelData_BodyType.ModelData_BT_Sheet:
aNewName = "Sheet Body";
break;
case ModelData_BodyType.ModelData_BT_Solid:
aNewName = "Solid Body";
break;
case ModelData_BodyType.ModelData_BT_General:
aNewName = "General Body";
break;
case ModelData_BodyType.ModelData_BT_Undefined:
aNewName = "Undefined Body";
break;
}
aNewName += " " + aBRep.ShapeId(aBodyList.Element(i)).ToString();
aBRep.SetName(aBodyList.Element(i), new Base_UTF16String(aNewName));
}
}
public override void Apply(ModelData_PolyRepresentation thePolyRep)
{
ModelData_PolyRepresentation aPolyRep = thePolyRep;
ModelData_PolyShapeList aPolyList = aPolyRep.Get();
int anITSId = 0;
int aPLSId = 0;
int aPPSId = 0;
for (uint i = 0; i < aPolyList.Size(); ++i)
{
String aNewName = "aNewName";
if (aPolyList.Element(i).TypeId() == ModelData_IndexedTriangleSet.GetTypeId())
{
++anITSId;
aNewName = "Indexed Triangle Set";
ModelData_IndexedTriangleSet anITS = ModelData_IndexedTriangleSet.Cast(aPolyList.Element(i));
aNewName += " " + anITSId.ToString();
anITS.SetName(new Base_UTF16String(aNewName));
}
else if (aPolyList.Element(i).TypeId() == ModelData_PolyLineSet.GetTypeId())
{
++aPLSId;
aNewName = "Poly Line Set";
ModelData_PolyLineSet aPLS = ModelData_PolyLineSet.Cast(aPolyList.Element(i));
aNewName += " " + aPLSId.ToString();
aPLS.SetName(new Base_UTF16String(aNewName));
}
else if (aPolyList.Element(i).TypeId() == ModelData_PolyPointSet.GetTypeId())
{
++aPPSId;
aNewName = "Poly Point Set";
ModelData_PolyPointSet aPPS = ModelData_PolyPointSet.Cast(aPolyList.Element(i));
aNewName += " " + aPPSId.ToString();
aPPS.SetName(new Base_UTF16String(aNewName));
}
}
}
}
class SceneGraphNameModifier : ModelData_Model.VoidElementVisitor
{
public SceneGraphNameModifier(int thePartId = 0, int theAssemblyId = 0, int theInstanceId = 0)
{
myPartId = thePartId;
myAssemblyId = theAssemblyId;
myInstanceId = theInstanceId;
}
public override void Apply(ModelData_Part thePart)
{
++myPartId;
ModelData_Part aPart = thePart;
String aNewName = "Part " + myPartId.ToString();
aPart.SetName(new Base_UTF16String(aNewName));
RepresentationNameModifier aModifier = new RepresentationNameModifier();
thePart.Accept(aModifier);
}
public override bool VisitEnter(ModelData_Assembly theAssembly)
{
if (!myAssemblyMap.ContainsKey(theAssembly))
{
++myAssemblyId;
myAssemblyMap.Add(theAssembly, myAssemblyId);
ModelData_Assembly anAssembly = theAssembly;
String aNewName = "Assembly " + myAssemblyId.ToString();
anAssembly.SetName(new Base_UTF16String(aNewName));
}
return true;
}
public override bool VisitEnter(ModelData_Instance theInstance)
{
++myInstanceId;
ModelData_Instance anInstance = theInstance;
String aNewName = "Instance " + myInstanceId.ToString();
anInstance.SetName(new Base_UTF16String(aNewName));
return true;
}
private int myPartId;
private int myAssemblyId;
private int myInstanceId;
private Dictionary<ModelData_Assembly, int> myAssemblyMap = new Dictionary<ModelData_Assembly, int>();
}
}
}
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition: Base_UTF16String.hxx:34
Defines a group of scene graph element.
Definition: ModelData_Assembly.hxx:33
Defines precise Boundary Representation of part.
Definition: ModelData_BRepRepresentation.hxx:39
int ShapeId(const ModelData_Shape &theShape) const
Returns an Id of B-Rep shape.
Definition: ModelData_BRepRepresentation.cxx:1021
void SetName(const ModelData_Shape &theSubshape, const Base_UTF16String &theName)
Definition: ModelData_BRepRepresentation.cxx:684
void Accept(SubshapeVisitor &theVisitor) const
Applies the visitor to visit the subshapes.
Definition: ModelData_BRepRepresentation.cxx:993
const ModelData_BodyList & Get() const
Returns an associated topological object.
Definition: ModelData_BRepRepresentation.cxx:626
IdType TypeId() const
Returns an object type id.
Definition: ModelData_BaseObject.cxx:199
void SetName(const Base_UTF16String &theName)
Sets an object name.
Definition: ModelData_BaseObject.cxx:209
ModelData_BodyType BodyType() const
Returns a body type.
Definition: ModelData_Body.cxx:139
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
Defines an occurrence of assembly or part in a scene graph.
Definition: ModelData_Instance.hxx:34
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
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
void Accept(RepresentationVisitor &theVisitor) const
Accepts a visitor of the associated representations.
Definition: ModelData_Part.cxx:384
Defines a polygonal shape consisting of polylines.
Definition: ModelData_PolyLineSet.hxx:31
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
Defines a visitor that visits each unique element only once.
Definition: ModelData_SceneGraphElementUniqueVisitor.hxx:33
Base class of topological shapes.
Definition: ModelData_Shape.hxx:37
ModelData_ShapeType Type() const
Returns a shape type.
Definition: ModelData_Shape.cxx:358
Defines classes, types, and global functions related to CAD Exchanger.
Definition: A3DSTestLib.hxx:22
ModelData_ShapeType
Defines shape type.
Definition: ModelData_ShapeType.hxx:25
ModelData_BodyType
Defines a body type.
Definition: ModelData_BodyType.hxx:25