Hide menu
Loading...
Searching...
No Matches
modification/coloring/main.cxx

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.
//
// ****************************************************************************
#include <cadex/LicenseManager_Activate.h>
#include <cadex/ModelData_Appearance.hxx>
#include <cadex/ModelData_Assembly.hxx>
#include <cadex/ModelData_Body.hxx>
#include <cadex/ModelData_BodyList.hxx>
#include <cadex/ModelData_BRepRepresentation.hxx>
#include <cadex/ModelData_Color.hxx>
#include <cadex/ModelData_IndexedTriangleSet.hxx>
#include <cadex/ModelData_Instance.hxx>
#include <cadex/ModelData_LineProperties.hxx>
#include <cadex/ModelData_Material.hxx>
#include <cadex/ModelData_Model.hxx>
#include <cadex/ModelData_ModelReader.hxx>
#include <cadex/ModelData_ModelWriter.hxx>
#include <cadex/ModelData_Part.hxx>
#include <cadex/ModelData_PolyRepresentation.hxx>
#include <cadex/ModelData_PolyShapeList.hxx>
#include <cadex/ModelData_SceneGraphElementUniqueVisitor.hxx>
#include <iostream>
#include "../../cadex_license.cxx"
using namespace std;
using namespace cadex;
// Creates a "unique" color
ModelData_Color CreateColor (size_t theColorId) {
int r = (theColorId * 61) % 256;
int g = (theColorId * 101) % 256;
int b = (theColorId * 151) % 256;
int a = 255;
return ModelData_Color (r, g, b, a);
}
ModelData_Appearance CreateAppearance (size_t theAppearanceId) {
ModelData_Material aMaterial;
aMaterial.DiffuseColor() = CreateColor (theAppearanceId);
aMaterial.AmbientColor() = ModelData_Color (0.24725f, 0.1995f, 0.0745f);
aMaterial.SpecularColor() = ModelData_Color (0.75164f, 0.60648f, 0.22648f);
aMaterial.EmissionColor() = ModelData_Color (0.2f, 0.2f, 0.2f);
aMaterial.Shininess() = 83.2f;
ModelData_Appearance anAppearance (aMaterial);
ModelData_LineProperties aLineProperties;
aLineProperties.Type() = ModelData_LineProperties::Dotted;
anAppearance.Set (aLineProperties);
return anAppearance;
}
class SubshapeAppearanceModifier : public ModelData_BRepRepresentation::SubshapeVisitor
{
public:
SubshapeAppearanceModifier (const ModelData_BRepRepresentation& theBRep, size_t theAppearanceId) :
myBRep (theBRep), myAppearanceId (theAppearanceId) {}
protected:
void operator() (const ModelData_Shape& theShape) override
{
ModelData_Appearance anAppearance = myBRep.Appearance (theShape);
if (anAppearance) {
myBRep.SetAppearance (theShape, CreateAppearance (myAppearanceId));
}
}
private:
size_t myAppearanceId;
};
class RepresentationAppearanceModifier : public ModelData_Part::RepresentationVisitor
{
public:
RepresentationAppearanceModifier (size_t theAppearanceId) : myAppearanceId (theAppearanceId) {}
protected:
void operator() (const ModelData_BRepRepresentation& theBRep) override
{
const ModelData_BodyList& aBodyList = aBRep.Get();
for (ModelData_BodyList::SizeType i = 0; i < aBodyList.Size(); ++i) {
ModelData_Appearance anAppearance = aBRep.Appearance (aBodyList[i]);
if (anAppearance) {
aBRep.SetAppearance (aBodyList[i], CreateAppearance (myAppearanceId));
}
}
SubshapeAppearanceModifier aModifier (theBRep, myAppearanceId);
theBRep.Accept (aModifier);
}
void operator() (const ModelData_PolyRepresentation& thePolyRep) override
{
ModelData_PolyRepresentation aPolyRep = thePolyRep;
const ModelData_PolyShapeList& aPolyList = aPolyRep.Get();
for (ModelData_PolyShapeList::SizeType i = 0; i < aPolyList.Size(); ++i) {
if (aPolyList[i].IsOfType<ModelData_IndexedTriangleSet>()) {
ModelData_IndexedTriangleSet anITS = static_cast<const ModelData_IndexedTriangleSet&> (aPolyList[i]);
ModelData_Appearance anAppearance = anITS.Appearance();
if (anAppearance) {
anITS.SetAppearance (CreateAppearance (myAppearanceId));
}
}
}
}
private:
size_t myAppearanceId;
};
class SceneGraphAppearanceModifier : public ModelData_Model::VoidElementVisitor
{
public:
SceneGraphAppearanceModifier (size_t theAppearanceId = 0) : myAppearanceId (theAppearanceId) {}
protected:
void operator() (const ModelData_Part& thePart) override
{
ModelData_Part aPart = thePart;
ModelData_Appearance anAppearance = aPart.Appearance();
if (anAppearance) {
myAppearanceId++;
aPart.SetAppearance (CreateAppearance (myAppearanceId));
}
RepresentationAppearanceModifier aModifier (myAppearanceId);
thePart.Accept (aModifier);
}
bool VisitEnter (const ModelData_Assembly& theAssembly) override
{
ModelData_Assembly anAssembly = theAssembly;
ModelData_Appearance anAppearance = anAssembly.Appearance();
if (anAppearance) {
myAppearanceId++;
anAssembly.SetAppearance (CreateAppearance (myAppearanceId));
}
return true;
}
bool VisitEnter (const ModelData_Instance& theInstance) override
{
ModelData_Instance anInstance = theInstance;
ModelData_Appearance anAppearance = anInstance.Appearance();
if (anAppearance) {
myAppearanceId++;
anInstance.SetAppearance (CreateAppearance (myAppearanceId));
}
return true;
}
private:
size_t myAppearanceId;
};
int main(int argc, char *argv[])
{
auto aKey = LicenseKey::Value();
// Activate the license (aKey must be defined in cadex_license.cxx)
if (!CADExLicense_Activate (aKey)) {
cerr << "Failed to activate CAD Exchanger license." << endl;
return 1;
}
if (argc != 3) {
cerr << "Usage: " << argv[0] << " <input_file> <output_file>, where:" << endl;
cerr << " <input_file> is a name of the model file to be read" << endl;
cerr << " <output_file> is a name of the model file to be written" << endl;
return 1;
}
const char* aSource = argv[1];
const char* aDest = argv[2];
// Opening and converting the file
if (!aReader.Read (aSource, aModel)) {
cerr << "Failed to open and convert the file " << aSource << endl;
return 1;
}
SceneGraphAppearanceModifier aModifier;
ModelData_SceneGraphElementUniqueVisitor aUniqueModifier (aModifier);
// Modifying the appearance of the model
aModel.Accept (aUniqueModifier);
// Converting and writing the model to file
if (!aWriter.Write (aModel, aDest)) {
cerr << "Failed to convert and write the file to specified format" << aDest << endl;
return 1;
}
return 0;
}
Provides an interface to appearance which is a collection of visual styles.
Definition: ModelData_Appearance.hxx:39
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
Defines a list of bodies.
Definition: ModelData_BodyList.hxx:31
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 Type() const
Definition: ModelData_LineProperties.hxx:55
Provides a data container for material.
Definition: ModelData_Material.hxx:28
const ModelData_Color & EmissionColor() const
Definition: ModelData_Material.hxx:55
const ModelData_Color & AmbientColor() const
Definition: ModelData_Material.hxx:52
const ModelData_Color & DiffuseColor() const
Definition: ModelData_Material.hxx:53
float Shininess() const
Definition: ModelData_Material.hxx:58
const ModelData_Color & SpecularColor() const
Definition: ModelData_Material.hxx:54
Element visitor with empty implementation.
Definition: ModelData_Model.hxx:113
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 visitor for model represenations.
Definition: ModelData_Part.hxx:79
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