Hide menu
Loading...
Searching...
No Matches
exploring/drawings/main.cxx

Refer to the Drawings 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.
//
// ****************************************************************************
#define __CADEX_PREVIEW_DRAWINGS 1
#include <cadex/Base_UTF16String.hxx>
#include <cadex/DWG_ReaderParameters.hxx>
#include <cadex/LicenseManager_Activate.h>
#include <cadex/ModelData_BaseObject.hxx>
#include <cadex/ModelData_Curve2d.hxx>
#include <cadex/ModelData_Drawing.hxx>
#include <cadex/ModelData_DrawingDimension.hxx>
#include <cadex/ModelData_DrawingElement.hxx>
#include <cadex/ModelData_DrawingElementVisitor.hxx>
#include <cadex/ModelData_DrawingGeometry.hxx>
#include <cadex/ModelData_DrawingHatch.hxx>
#include <cadex/ModelData_DrawingSheet.hxx>
#include <cadex/ModelData_DrawingText.hxx>
#include <cadex/ModelData_DrawingView.hxx>
#include <cadex/ModelData_Model.hxx>
#include <cadex/ModelData_ModelReader.hxx>
#include <iostream>
#include <unordered_map>
#include "../../cadex_license.cxx"
using namespace std;
using namespace cadex;
struct DrawingElementsHashType
{
size_t operator() (ModelData_BaseObject::IdType theIdType) const
{
return static_cast<size_t> (theIdType);
}
};
struct Curves2DHashType
{
size_t operator() (ModelData_CurveType theCurveType) const
{
return static_cast<size_t> (theCurveType);
}
};
typedef unordered_map<ModelData_BaseObject::IdType, size_t, DrawingElementsHashType> DrawingElementsMapType;
typedef unordered_map<ModelData_CurveType, size_t, Curves2DHashType> Curves2DMapType;
class DrawingElementsCounter : public ModelData_DrawingElementVoidVisitor
{
public:
DrawingElementsCounter(): myPoint2DNumber (0) {}
void operator() (const ModelData_DrawingAngularDimension& theElement) override
{
CountElement (theElement);
}
void operator() (const ModelData_DrawingCurveSet& theElement) override
{
CountElement (theElement);
for (size_t i = 0; i < theElement.NumberOfCurves(); i++) {
const ModelData_Curve2d& aCurve = theElement.Curve (i);
CountCurve2d (aCurve);
}
}
void operator() (const ModelData_DrawingDiametricDimension& theElement) override
{
CountElement (theElement);
}
void operator() (const ModelData_DrawingHatch& theElement) override
{
CountElement (theElement);
}
void operator() (const ModelData_DrawingLinearDimension& theElement) override
{
CountElement (theElement);
}
void operator() (const ModelData_DrawingPiecewiseContour& theElement) override
{
CountElement (theElement);
for (size_t i = 0; i < theElement.NumberOfCurves(); i++) {
const ModelData_Curve2d& aCurve = theElement.Curve (i);
CountCurve2d (aCurve);
}
}
void operator() (const ModelData_DrawingPointSet& theElement) override
{
CountElement (theElement);
myPoint2DNumber += theElement.NumberOfPoints();
}
void operator() (const ModelData_DrawingRadialDimension& theElement) override
{
CountElement (theElement);
}
void operator() (const ModelData_DrawingText& theElement) override
{
CountElement (theElement);
}
void Print() const
{
cout << endl << "Drawing Elements in the model view:" << endl;
for (const auto& anElement : myDrawingElementsNumber) {
PrintDrawingElementType (anElement.first);
cout << ": " << anElement.second << endl;
}
cout << endl << "2D Geometry in the model view:" << endl;
if (myPoint2DNumber) {
cout << "Points: " << myPoint2DNumber << endl;
}
for (const auto& aCurve : myCurves2DNumber) {
PrintCurveType (aCurve.first);
cout << ": " << aCurve.second << endl;
}
}
private:
// Count drawing elements in the model, grouped by type
void CountElement (const ModelData_DrawingElement& theElement)
{
ModelData_BaseObject::IdType aType = theElement.TypeId();
myDrawingElementsNumber.insert (make_pair (aType, 0));
myDrawingElementsNumber[aType]++;
}
// Count 2d curves in the model, grouped by type
void CountCurve2d (const ModelData_Curve2d& theCurve)
{
ModelData_CurveType aType = theCurve.Type();
myCurves2DNumber.insert (make_pair (aType, 0));
myCurves2DNumber[aType]++;
}
void PrintCurveType (const ModelData_CurveType& theType) const
{
switch (theType) {
case ModelData_CT_Line: cout << "Lines"; break;
case ModelData_CT_Circle: cout << "Circles"; break;
case ModelData_CT_Ellipse: cout << "Ellipses"; break;
case ModelData_CT_Hyperbola: cout << "Hyperbolas"; break;
case ModelData_CT_Parabola: cout << "Parabolas"; break;
case ModelData_CT_Bezier: cout << "Bezier Curves"; break;
case ModelData_CT_BSpline: cout << "BSpline Curves"; break;
case ModelData_CT_Offset: cout << "Offset Curves"; break;
case ModelData_CT_Trimmed: cout << "Trimmed Curves"; break;
default: cout << "Undefined"; break;
}
}
void PrintDrawingElementType (ModelData_BaseObject::IdType theType) const
{
if (theType == ModelData_DrawingAngularDimension::GetTypeId())
cout << "Angular Dimensions";
else if (theType == ModelData_DrawingDiametricDimension::GetTypeId())
cout << "Diametric Dimensions";
else if (theType == ModelData_DrawingHatch::GetTypeId())
cout << "Hatches";
else if (theType == ModelData_DrawingLinearDimension::GetTypeId())
cout << "Linear Dimensions";
else if (theType == ModelData_DrawingPiecewiseContour::GetTypeId())
cout << "Piecewise Contours";
else if (theType == ModelData_DrawingPointSet::GetTypeId())
cout << "Point Sets";
else if (theType == ModelData_DrawingRadialDimension::GetTypeId())
cout << "Radial Dimensions";
else if (theType == ModelData_DrawingText::GetTypeId())
cout << "Texts";
else if (theType == ModelData_DrawingCurveSet::GetTypeId())
cout << "Curve Sets";
else
cout << "Undefined elements";
}
DrawingElementsMapType myDrawingElementsNumber;
Curves2DMapType myCurves2DNumber;
size_t myPoint2DNumber;
};
Base_UTF16String GetName (const ModelData_BaseObject& theObject)
{
if (theObject.Name().IsEmpty()) {
return "Unnamed";
}
return theObject.Name();
}
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 != 2) {
cerr << "Usage: " << argv[0] << " <input_file>, where:" << endl;
cerr << " <input_file> is a name of the DWG file to Read() model from" << endl;
return 1;
}
const char* aSource = argv[1];
// Enable reading drawing data
aParams.ReadDrawing() = true;
aReader.SetReaderParameters (aParams);
// Opening and converting the file
if (!aReader.Read (aSource, aModel)) {
cerr << "Failed to open and convert the file " << aSource << endl;
return 1;
}
// Drawing structure traversal
ModelData_Drawing aDrawing = aModel.Drawing();
if (!aDrawing) {
cout << "No drawings found in the model" << endl;
return 0;
}
cout << "Drawing <" << GetName (aDrawing) << ">: " <<
aDrawing.NumberOfSheets() << " sheets" << endl;
// Sheets traversal with an iterator
for (ModelData_Drawing::SheetIterator aSheetIt (aDrawing); aSheetIt.HasNext();) {
ModelData_DrawingSheet aSheet = aSheetIt.Next();
cout << "Sheet <" << GetName (aSheet) << ">: " <<
aSheet.NumberOfViews() << " views" << endl;
// Views traversal with an iterator
for (ModelData_DrawingSheet::ViewIterator aViewIt (aSheet); aViewIt.HasNext();) {
const ModelData_DrawingView& aView = aViewIt.Next();
cout << "View <" << GetName (aView) << ">: " <<
aView.NumberOfElements() << " elements" << endl;
// Drawing contents traversal with a visitor
DrawingElementsCounter aCounter;
aView.Accept (aCounter);
aCounter.Print();
}
}
return 0;
}
bool ReadDrawing() const
Specifies whether a drawing should be read from the file. This is an overloaded member function,...
Definition: Base_ReaderParameters.cxx:194
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
Defines parameters of the DWG reader.
Definition: DWG_ReaderParameters.hxx:27
Base class for all objects that have private implementation (pimpl) back-end.
Definition: ModelData_BaseObject.hxx:34
IdType TypeId() const
Returns an object type id.
Definition: ModelData_BaseObject.cxx:199
Base_UTF16String Name() const
Definition: ModelData_BaseObject.cxx:218
int IdType
Defines a type identifier.
Definition: ModelData_BaseObject.hxx:42
Base class for 2D curves.
Definition: ModelData_Curve2d.hxx:42
ModelData_CurveType Type() const
Returns a curve type.
Definition: ModelData_Curve2d.cxx:128
Iterator over sheets of a drawing.
Definition: ModelData_Drawing.hxx:51
bool HasNext() const
Returns true if the iterator can be advanced with Next().
Definition: ModelData_Drawing.cxx:112
Describes the angular dimension measures of for an angle given by three points.
Definition: ModelData_DrawingDimension.hxx:70
Describes drawing elements composed of 2D curves.
Definition: ModelData_DrawingGeometry.hxx:48
size_t NumberOfCurves() const
Returns the number of curves currently composing the element.
Definition: ModelData_DrawingGeometry.cxx:78
const ModelData_Curve2d & Curve(size_t theIndex) const
Returns the specified curve of the element.
Definition: ModelData_DrawingGeometry.cxx:88
Describes a diametric dimension measures of circles.
Definition: ModelData_DrawingDimension.hxx:98
Base class for drawing elements.
Definition: ModelData_DrawingElement.hxx:36
Defines a visitor of drawing elements with empty implementation.
Definition: ModelData_DrawingElementVisitor.hxx:56
Represents an area filled with a pattern.
Definition: ModelData_DrawingHatch.hxx:34
Represents a single 2D drawing of a model.
Definition: ModelData_Drawing.hxx:37
size_t NumberOfSheets() const
Returns the number of sheets in the drawing.
Definition: ModelData_Drawing.cxx:51
Linear dimensions with horizontal, vertical, and aligned dimension lines.
Definition: ModelData_DrawingDimension.hxx:152
Describes drawing elements composed connected 2D curves.
Definition: ModelData_DrawingGeometry.hxx:78
size_t NumberOfCurves() const
Returns the number of curves currently composing the element.
Definition: ModelData_DrawingGeometry.cxx:215
const ModelData_Curve2d & Curve(size_t theIndex) const
Returns the specified curve of the element.
Definition: ModelData_DrawingGeometry.cxx:225
Describes drawing elements composed of 2D points.
Definition: ModelData_DrawingGeometry.hxx:63
size_t NumberOfPoints() const
Returns the number of points currently composing the element.
Definition: ModelData_DrawingGeometry.cxx:127
Describes a radial dimension measures of arcs and circles.
Definition: ModelData_DrawingDimension.hxx:125
Iterates over views of a drawing sheet.
Definition: ModelData_DrawingSheet.hxx:79
bool HasNext() const
Returns true if the iterator can be advanced with Next().
Definition: ModelData_DrawingSheet.cxx:221
Represents a single sheet of a model drawing.
Definition: ModelData_DrawingSheet.hxx:37
size_t NumberOfViews() const
Returns the number of child views.
Definition: ModelData_DrawingSheet.cxx:177
Defines a text for drawing.
Definition: ModelData_DrawingText.hxx:38
Represents a view on a drawing sheet.
Definition: ModelData_DrawingView.hxx:39
size_t NumberOfElements() const
Returns the number of elements in the view.
Definition: ModelData_DrawingView.cxx:114
Provides CAD Exchanger data model.
Definition: ModelData_Model.hxx:43
ModelData_Drawing Drawing() const
Returns the drawing of the model.
Definition: ModelData_Model.cxx:870
Reads any format that CAD Exchanger can import.
Definition: ModelData_ModelReader.hxx:33
void SetReaderParameters(const Base_ReaderParameters &theParameters)
Sets reader parameters.
Definition: ModelData_ModelReader.cxx:216
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
Defines classes, types, and global functions related to CAD Exchanger.
Definition: A3DSTestLib.hxx:22
ModelData_CurveType
Defines curve type.
Definition: ModelData_CurveType.hxx:25