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

Refer to the Coloring 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 coloring
{
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;
}
SceneGraphAppearanceModifier aModifier = new SceneGraphAppearanceModifier();
// Modifying the appearance of the model
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;
}
// Creates a "unique" color
public static ModelData_Color CreateColor(int theColorId)
{
int r = (theColorId * 61) % 256;
int g = (theColorId * 101) % 256;
int b = (theColorId * 151) % 256;
int a = 255;
return new ModelData_Color(r, g, b, a);
}
public static ModelData_Appearance CreateAppearance(int theAppearanceId)
{
aMaterial.SetDiffuseColor(CreateColor(theAppearanceId));
aMaterial.SetAmbientColor(new ModelData_Color(0.24725f, 0.1995f, 0.0745f));
aMaterial.SetSpecularColor(new ModelData_Color(0.75164f, 0.60648f, 0.22648f));
aMaterial.SetEmissionColor(new ModelData_Color(0.2f, 0.2f, 0.2f));
aMaterial.SetShininess(83.2f);
ModelData_Appearance anAppearance = new ModelData_Appearance(aMaterial);
aLineProperties.SetType(ModelData_LineProperties.LineType.Dotted);
anAppearance.Set(aLineProperties);
return anAppearance;
}
class SubshapeAppearanceModifier : ModelData_BRepRepresentation.SubshapeVisitor
{
public SubshapeAppearanceModifier(ModelData_BRepRepresentation theBRep, int theAppearanceId)
{
myBRep = theBRep;
myAppearanceId = theAppearanceId;
}
public override void Apply(ModelData_Shape theShape)
{
ModelData_Appearance anAppearance = myBRep.Appearance(theShape);
if (anAppearance != null)
{
myBRep.SetAppearance(theShape, CreateAppearance(myAppearanceId));
}
}
private int myAppearanceId;
}
class RepresentationAppearanceModifier : ModelData_Part.RepresentationVisitor
{
public RepresentationAppearanceModifier(int theAppearanceId)
{
myAppearanceId = theAppearanceId;
}
public override void Apply(ModelData_BRepRepresentation theBRep)
{
ModelData_BodyList aBodyList = aBRep.Get();
for (uint i = 0; i < aBodyList.Size(); ++i)
{
ModelData_Appearance anAppearance = aBRep.Appearance(aBodyList.Element(i));
if (anAppearance != null)
{
aBRep.SetAppearance(aBodyList.Element(i), anAppearance);
}
}
SubshapeAppearanceModifier aModifier = new SubshapeAppearanceModifier(theBRep, myAppearanceId);
theBRep.Accept(aModifier);
}
public override void Apply(ModelData_PolyRepresentation thePolyRep)
{
ModelData_PolyRepresentation aPolyRep = thePolyRep;
ModelData_PolyShapeList aPolyList = aPolyRep.Get();
for (uint i = 0; i < aPolyList.Size(); ++i)
{
if (aPolyList.Element(i).TypeId() == ModelData_IndexedTriangleSet.GetTypeId())
{
ModelData_IndexedTriangleSet anITS = ModelData_IndexedTriangleSet.Cast(aPolyList.Element(i));
ModelData_Appearance anAppearance = anITS.Appearance();
if (anAppearance != null)
{
anITS.SetAppearance(CreateAppearance(myAppearanceId));
}
}
}
}
private int myAppearanceId;
}
class SceneGraphAppearanceModifier : ModelData_Model.VoidElementVisitor
{
public SceneGraphAppearanceModifier(int theAppearanceId = 0)
{
myAppearanceId = theAppearanceId;
}
public override void Apply(ModelData_Part thePart)
{
ModelData_Part aPart = thePart;
ModelData_Appearance anAppearance = aPart.Appearance();
if (anAppearance != null)
{
myAppearanceId++;
aPart.SetAppearance(CreateAppearance(myAppearanceId));
}
RepresentationAppearanceModifier aModifier = new RepresentationAppearanceModifier(myAppearanceId);
thePart.Accept(aModifier);
}
public override bool VisitEnter(ModelData_Assembly theAssembly)
{
ModelData_Assembly anAssembly = theAssembly;
ModelData_Appearance anAppearance = anAssembly.Appearance();
if (anAppearance != null)
{
myAppearanceId++;
anAssembly.SetAppearance(CreateAppearance(myAppearanceId));
}
return true;
}
public override bool VisitEnter(ModelData_Instance theInstance)
{
ModelData_Instance anInstance = theInstance;
ModelData_Appearance anAppearance = anInstance.Appearance();
if (anAppearance != null)
{
myAppearanceId++;
anInstance.SetAppearance(CreateAppearance(myAppearanceId));
}
return true;
}
private int myAppearanceId;
}
}
}
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition: Base_UTF16String.hxx:34
Provides an interface to appearance which is a collection of visual styles.
Definition: ModelData_Appearance.hxx:39
void Set(const ModelData_Color &theGenericColor)
Sets the color of appearance.
Definition: ModelData_Appearance.cxx:174
Defines a group of scene graph element.
Definition: ModelData_Assembly.hxx:33
Defines precise Boundary Representation of part.
Definition: ModelData_BRepRepresentation.hxx:39
void SetAppearance(const ModelData_Shape &theSubshape, const ModelData_Appearance &theAppearance)
Definition: ModelData_BRepRepresentation.cxx:698
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
Defines a list of bodies.
Definition: ModelData_BodyList.hxx:31
const ModelData_Body & Element(SizeType theIndex) const
Definition: ModelData_BodyList.cxx:177
Defines an RGBA color (with alpha channel).
Definition: ModelData_Color.hxx:34
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
Specifies properties to be applied to lines.
Definition: ModelData_LineProperties.hxx:27
LineType
Defines the line type in accordance with International Register of Graphical Items.
Definition: ModelData_LineProperties.hxx:33
Provides a data container for material.
Definition: ModelData_Material.hxx:28
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 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
void SetAppearance(const ModelData_Appearance &theAppearance)
Definition: ModelData_PolyVertexSet.cxx:71
ModelData_Appearance Appearance() const
Returns the scene graph element's own appearance.
Definition: ModelData_SceneGraphElement.cxx:120
void SetAppearance(const ModelData_Appearance &theAppearance)
Sets appearance of the scene graph element.
Definition: ModelData_SceneGraphElement.cxx:195
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
Defines classes, types, and global functions related to CAD Exchanger.
Definition: A3DSTestLib.hxx:22