Hide menu
Loading...
Searching...
No Matches
exploring/drawings/Program.cs

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.
//
// ****************************************************************************
using cadex;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace drawings
{
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 != 1)
{
Console.WriteLine("Usage: " + System.Reflection.Assembly.GetExecutingAssembly().Location
+ " <input_file>, where:");
Console.WriteLine(" <input_file> is a name of the DWG file to be read");
return 1;
}
string aSource = args[0];
// Open the model
//Enable reading drawing data
aParams.SetReadDrawing(true);
aReader.SetReaderParameters(aParams);
if (!aReader.Read(new Base_UTF16String(aSource), aModel))
{
Console.WriteLine("Failed to read the file " + aSource);
return 1;
}
// Drawing structure traversal
ModelData_Drawing aDrawing = aModel.Drawing();
if (aDrawing == null)
{
Console.WriteLine("No drawings found in the model");
return 0;
}
Console.Write("Drawing <" + GetName(aDrawing) + ">: ");
Console.WriteLine(aDrawing.NumberOfSheets() + " sheets");
// Sheets traversal with an iterator
while (aSheetIt.HasNext())
{
ModelData_DrawingSheet aSheet = aSheetIt.Next();
Console.WriteLine("Sheet <" + GetName(aSheet) + ">: " +
aSheet.NumberOfViews() + " views");
// Views traversal with an iterator
while (aViewIt.HasNext())
{
ModelData_DrawingView aView = aViewIt.Next();
Console.WriteLine("View <" + GetName(aView) + ">: " +
aView.NumberOfElements() + " elements");
// Drawing contents traversal with a visitor
DrawingElementCounter aCounter = new DrawingElementCounter();
aView.Accept(aCounter);
aCounter.Print();
}
}
return 0;
}
static Base_UTF16String GetName(ModelData_BaseObject theObject)
{
if (theObject.Name().IsEmpty())
{
return new Base_UTF16String("Unnamed");
}
return theObject.Name();
}
class DrawingElementCounter : ModelData_DrawingElementVoidVisitor
{
public DrawingElementCounter()
{
myPoint2DNumber = 0;
}
public override void Apply(ModelData_DrawingAngularDimension theElement)
{
CountElement(theElement);
}
public override void Apply(ModelData_DrawingCurveSet theElement)
{
CountElement(theElement);
for (uint i = 0; i < theElement.NumberOfCurves(); ++i)
{
ModelData_Curve2d aCurve = theElement.Curve(i);
CountCurve2d(aCurve);
}
}
public override void Apply(ModelData_DrawingDiametricDimension theElement)
{
CountElement(theElement);
}
public override void Apply(ModelData_DrawingHatch theElement)
{
CountElement(theElement);
}
public override void Apply(ModelData_DrawingLinearDimension theElement)
{
CountElement(theElement);
}
public override void Apply(ModelData_DrawingPiecewiseContour theElement)
{
CountElement(theElement);
for (uint i = 0; i < theElement.NumberOfCurves(); ++i)
{
ModelData_Curve2d aCurve = theElement.Curve(i);
CountCurve2d(aCurve);
}
}
public override void Apply(ModelData_DrawingPointSet theElement)
{
CountElement(theElement);
myPoint2DNumber += theElement.NumberOfPoints();
}
public override void Apply(ModelData_DrawingRadialDimension theElement)
{
CountElement(theElement);
}
public override void Apply(ModelData_DrawingText theElement)
{
CountElement(theElement);
}
public void Print()
{
Console.WriteLine();
Console.WriteLine("Drawing Elements in the model view:");
foreach (KeyValuePair<int,int> anElement in myDrawingElementsNumber)
{
PrintDrawingElementType(anElement.Key);
Console.WriteLine(": " + anElement.Value);
}
Console.WriteLine();
Console.WriteLine("2D Geometry in the model view:");
if (myPoint2DNumber != 0)
{
Console.WriteLine("Points: " + myPoint2DNumber);
}
foreach (KeyValuePair<ModelData_CurveType, int> aCurve in myCurves2DNumber)
{
PrintCurveType(aCurve.Key);
Console.WriteLine(": " + aCurve.Value);
}
}
// Count drawing elements in the model, grouped by type
private void CountElement(ModelData_DrawingElement theElement)
{
int aType = theElement.TypeId();
if (!myDrawingElementsNumber.ContainsKey(aType))
{
myDrawingElementsNumber.Add(aType, 1);
}
else
{
myDrawingElementsNumber[aType]++;
}
}
private void CountCurve2d(ModelData_Curve2d theCurve)
{
ModelData_CurveType aType = theCurve.Type();
if (!myCurves2DNumber.ContainsKey(aType))
{
myCurves2DNumber.Add(aType, 1);
}
else
{
myCurves2DNumber[aType]++;
}
}
// Count 2d curves in the model, grouped by type
private void PrintCurveType(ModelData_CurveType theType)
{
switch (theType)
{
case ModelData_CurveType.ModelData_CT_Line: Console.Write("Lines"); break;
case ModelData_CurveType.ModelData_CT_Circle: Console.Write("Circles"); break;
case ModelData_CurveType.ModelData_CT_Ellipse: Console.Write("Ellipses"); break;
case ModelData_CurveType.ModelData_CT_Hyperbola: Console.Write("Hyperbolas"); break;
case ModelData_CurveType.ModelData_CT_Parabola: Console.Write("Parabolas"); break;
case ModelData_CurveType.ModelData_CT_Bezier: Console.Write("Bezier Curves"); break;
case ModelData_CurveType.ModelData_CT_BSpline: Console.Write("BSpline Curves"); break;
case ModelData_CurveType.ModelData_CT_Offset: Console.Write("Offset Curves"); break;
case ModelData_CurveType.ModelData_CT_Trimmed: Console.Write("Trimmed Curves"); break;
default: Console.Write("Undefined"); break;
}
}
private void PrintDrawingElementType(int theType)
{
if (theType == ModelData_DrawingAngularDimension.GetTypeId())
Console.Write("Angular Dimensions");
else if (theType == ModelData_DrawingDiametricDimension.GetTypeId())
Console.Write("Diametric Dimensions");
else if (theType == ModelData_DrawingHatch.GetTypeId())
Console.Write("Hatches");
else if (theType == ModelData_DrawingLinearDimension.GetTypeId())
Console.Write("Linear Dimensions");
else if (theType == ModelData_DrawingPiecewiseContour.GetTypeId())
Console.Write("Piecewise Contours");
else if (theType == ModelData_DrawingPointSet.GetTypeId())
Console.Write("Point Sets");
else if (theType == ModelData_DrawingRadialDimension.GetTypeId())
Console.Write("Radial Dimensions");
else if (theType == ModelData_DrawingText.GetTypeId())
Console.Write("Texts");
else if (theType == ModelData_DrawingCurveSet.GetTypeId())
Console.Write("Curve Sets");
else
Console.WriteLine("Undefined elements");
}
private Dictionary<int, int> myDrawingElementsNumber = new Dictionary<int, int>();
private Dictionary<ModelData_CurveType, int> myCurves2DNumber = new Dictionary<ModelData_CurveType, int>();
private uint myPoint2DNumber;
}
}
}
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
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
const ModelData_DrawingSheet & Next()
Returns the current sheet and advances the iterator.
Definition: ModelData_Drawing.cxx:118
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
const ModelData_DrawingView & Next()
Returns the current view and advances the iterator.
Definition: ModelData_DrawingSheet.cxx:227
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