Hide menu
Loading...
Searching...
No Matches
modeling/assembly/Program.cs

Refer to the Assembly Modeling 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 assembly
{
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;
}
// Initial Parameters
double aRadius = 0.8;
ModelData_VectorList aPos = new ModelData_VectorList
{
new ModelData_Vector (aRadius * -2, aRadius * 1.5, 0.0),
new ModelData_Vector (aRadius * 2, aRadius * 1.5, 0.0),
new ModelData_Vector (aRadius * 0, aRadius * 4.5, 0.0)
};
// Create shapes
ModelData_Part aNut = CreateNut(aRadius);
ModelData_Part aBolt = CreateBolt(aRadius);
// Create assemblies
ModelData_Assembly aNutBoltAssembly = CreateNutBoltAssembly(aNut, aBolt, -aRadius * 4.5);
// Create a model a fill its roots
// Transformations
for (int i = 0; i < aPos.Count; ++i)
{
ModelData_Transformation anAssemblyTrsf = new ModelData_Transformation(aPos[i]);
ModelData_Instance anInstance = new ModelData_Instance(aNutBoltAssembly, anAssemblyTrsf, new Base_UTF16String("Nut-Bolt Assembly"));
aModel.AddRoot(anInstance);
}
aWriter.Write(aModel, new Base_UTF16String("assembly.cdx"));
return 0;
}
static ModelData_Direction MakeDirection(ModelData_Vector theVector)
{
ModelData_Vectord aVector = theVector.Normalized();
return new ModelData_Direction(aVector.X(), aVector.Y(), aVector.Z());
}
static ModelData_Face MakeCircularFace(ModelData_Point thePos, ModelData_Direction theDir, ModelData_Edge theBoundary)
{
ModelData_Plane aPlane = new ModelData_Plane(a3Axis);
ModelData_Face aFace = new ModelData_Face(aPlane);
ModelData_Wire aWire = new ModelData_Wire(theBoundary);
aFace.Append(aWire);
return aFace;
}
static ModelData_Face MakeCircularFaceWithInnerWire(ModelData_Point thePos, ModelData_Direction theDir,
ModelData_Edge theInner, ModelData_Edge theOuter)
{
ModelData_Plane aPlane = new ModelData_Plane(a3Axis);
ModelData_Face aFace = new ModelData_Face(aPlane);
ModelData_Wire anInner = new ModelData_Wire(theInner);
ModelData_Wire anOuter = new ModelData_Wire(theOuter);
aFace.Append(anInner);
aFace.Append(anOuter);
return aFace;
}
static ModelData_Face MakeCylindricalFace(ModelData_Point thePos, ModelData_Direction theDir, double theRadius,
double theLength, ref ModelData_Edge theTopEdge, ref ModelData_Edge theBottomEdge)
{
ModelData_Face aFace = new ModelData_Face(aSurface, 0, Math.PI * 2, 0, theLength);
// Get outer edges
ModelData_Wire aWire = aFace.OuterWire();
while (anIt.HasNext())
{
ModelData_Edge anEdge = ModelData_Edge.Cast(anIt.Next());
ModelData_Point aP1 = anEdge.EndVertex().Point();
ModelData_Point aP2 = anEdge.StartVertex().Point();
if (aP1.IsEqual(aP2, new ModelData_Vertex(aP2).Tolerance()))
{
// The edge is closed
if (aP1.Z() == (theLength * theDir.Z()))
{
theTopEdge = anEdge;
}
else
{
theBottomEdge = anEdge;
}
}
}
return aFace;
}
static ModelData_Part CreateNut(double theRadius)
{
double aNutA = theRadius * 2, aNutB = theRadius * 1.5, aHeight = theRadius / 2;
ModelData_Point anInitPosition = new ModelData_Point(0.0, 0.0, -aHeight / 2);
ModelData_Plane aPlane = new ModelData_Plane(a3Axis);
ModelData_Face aFace = new ModelData_Face(aPlane, -aNutA, aNutA, -aNutB, aNutB);
ModelData_Circle aCircle = new ModelData_Circle(a2Axis, theRadius);
ModelData_Edge anEdge = new ModelData_Edge(aCircle);
ModelData_Wire anInnerWire = new ModelData_Wire(anEdge);
aFace.Append(ModelData_Wire.Cast(anInnerWire.Reversed()));
ModelData_Shape aNutShape = ModelAlgo_BRepFeatures.CreateExtrusion(aFace, new ModelData_Vector(0.0, 0.0, aHeight));
// Add red color
ModelData_Color aColor = new ModelData_Color(255, 0, 0);
aBRep.SetAppearance(aNutShape, anApp);
ModelData_Part aNut = new ModelData_Part(aBRep, new Base_UTF16String("Nut"));
return aNut;
}
static ModelData_Part CreateBolt(double theRadius)
{
double aMajorRadius = theRadius * 1.3;
double aLongLength = theRadius * 6;
double aShortLength = theRadius / 2;
// Cylindrical faces and their edges
ModelData_Edge aTopEdge = null;
ModelData_Edge aMiddleOuterEdge = null;
ModelData_Edge aMiddleInnerEdge = null;
ModelData_Edge aBottomEdge = null;
ModelData_Face aFace1 = MakeCylindricalFace(new ModelData_Point(0.0, 0.0, 0.0), ModelData_Direction.ZDir(),
aMajorRadius, aShortLength, ref aTopEdge, ref aMiddleOuterEdge);
ModelData_Face aFace2 = MakeCylindricalFace(new ModelData_Point(0.0, 0.0, 0.0), new ModelData_Direction(0.0, 0.0, -1.0),
theRadius, aLongLength, ref aBottomEdge, ref aMiddleInnerEdge);
// Top circle
ModelData_Face aFace3 = MakeCircularFace(new ModelData_Point(0.0, 0.0, aShortLength), ModelData_Direction.ZDir(),
// Face 4 with inner wire
ModelData_Face aFace4 = MakeCircularFaceWithInnerWire(new ModelData_Point(0.0, 0.0, 0.0), new ModelData_Direction(0.0, 0.0, -1.0),
ModelData_Edge.Cast(aMiddleInnerEdge.Reversed()),
ModelData_Edge.Cast(aMiddleOuterEdge.Reversed()));
// Bottom circle
ModelData_Face aFace5 = MakeCircularFace(new ModelData_Point(0.0, 0.0, -aLongLength), new ModelData_Direction(0.0, 0.0, -1.0),
ModelData_Edge.Cast(aBottomEdge.Reversed()));
ModelData_Shell aShell = new ModelData_Shell(aFace1);
aShell.Append(aFace2);
aShell.Append(aFace3);
aShell.Append(aFace4);
aShell.Append(aFace5);
ModelData_Solid aSolid = new ModelData_Solid(aShell);
// Add blue color
ModelData_Color aColor = new ModelData_Color(0, 0, 255);
aBRep.SetAppearance(aSolid, anApp);
ModelData_Part aBolt = new ModelData_Part(aBRep, new Base_UTF16String("Bolt"));
return aBolt;
}
static ModelData_Assembly CreateNutBoltAssembly(ModelData_Part theNut, ModelData_Part theBolt, double theDistance)
{
ModelData_Transformation aBoltTrsf = new ModelData_Transformation(new ModelData_Vector(0.0, 0.0, -theDistance));
ModelData_Assembly aNutBoltAssembly = new ModelData_Assembly(new Base_UTF16String("nut-bolt-assembly"));
aNutBoltAssembly.AddInstance(theNut, aNutTrsf);
aNutBoltAssembly.AddInstance(theBolt, aBoltTrsf);
return aNutBoltAssembly;
}
}
}
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition: Base_UTF16String.hxx:34
Creates B-Rep shapes using various feature modeling techniques.
Definition: ModelAlgo_BRepFeatures.hxx:44
static ModelData_Shape CreateExtrusion(const ModelData_Shape &theShape, const ModelData_Vector &theVec)
Creates an extrusion body.
Definition: ModelAlgo_BRepFeatures.cxx:84
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
const ModelData_Instance & AddInstance(const ModelData_SceneGraphElement &theElement, const Base_UTF16String &theInstanceName=Base_UTF16String())
Adds a new child element into the assembly.
Definition: ModelData_Assembly.cxx:117
Defines a right-hand axis placement in 3D.
Definition: ModelData_Axis2Placement.hxx:38
Defines a right-handed or left-handed axis placement in 3D.
Definition: ModelData_Axis3Placement.hxx:38
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
Defines 3D circle.
Definition: ModelData_Circle.hxx:33
Defines an RGBA color (with alpha channel).
Definition: ModelData_Color.hxx:34
Defines a cylindrical surface.
Definition: ModelData_CylindricalSurface.hxx:32
Defines a 3D direction.
Definition: ModelData_Direction.hxx:180
Defines an edge.
Definition: ModelData_Edge.hxx:36
static const ModelData_Edge & Cast(const ModelData_Shape &theShape)
Casts a base class object to ModelData_Edge.
Definition: ModelData_Edge.cxx:688
ModelData_Vertex EndVertex() const
Returns a start vertex.
Definition: ModelData_Edge.cxx:622
ModelData_Vertex StartVertex() const
Returns a start vertex.
Definition: ModelData_Edge.cxx:613
Defines a topological face.
Definition: ModelData_Face.hxx:32
ModelData_Wire OuterWire() const
Returns outer wire.
Definition: ModelData_Face.cxx:266
bool Append(const ModelData_Wire &theWire)
Adds a wire to the face.
Definition: ModelData_Face.cxx:226
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
const ModelData_SceneGraphElement & AddRoot(const ModelData_SceneGraphElement &theElement)
Adds new root element into the scene graph.
Definition: ModelData_Model.cxx:781
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:134
Defines a leaf node in the scene graph hiearchy.
Definition: ModelData_Part.hxx:35
Defines a plane.
Definition: ModelData_Plane.hxx:32
Defines a 3D point.
Definition: ModelData_Point.hxx:295
Iterates over subshapes in a shape.
Definition: ModelData_Shape.hxx:41
Base class of topological shapes.
Definition: ModelData_Shape.hxx:37
ModelData_Shape Reversed() const
Returns a shape that shares the same geometry and subshape graph but has opposite orientation.
Definition: ModelData_Shape.cxx:402
Defines a connected set of faces.
Definition: ModelData_Shell.hxx:31
bool Append(const ModelData_Face &theFace)
Adds a face to the shell.
Definition: ModelData_Shell.cxx:64
Defines a topological solid.
Definition: ModelData_Solid.hxx:31
Defines a transformation matrix.
Definition: ModelData_Transformation.hxx:33
Defines a 3D vector.
Definition: ModelData_Vector.hxx:442
Defines topological vertex.
Definition: ModelData_Vertex.hxx:31
ModelData_Point Point() const
Returns a 3D point this vertex resides at.
Definition: ModelData_Vertex.cxx:68
Defines a connected set of edges.
Definition: ModelData_Wire.hxx:31
Defines classes, types, and global functions related to CAD Exchanger.
Definition: A3DSTestLib.hxx:22
ModelData_ShapeType
Defines shape type.
Definition: ModelData_ShapeType.hxx:25