Hide menu
Loading...
Searching...
No Matches
modeling/assembly/assembly.py

Refer to the Assembly Modeling Example.

1#!/usr/bin/env python3
2
3# $Id$
4
5# Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
6# Copyright (C) 2014-2023, CADEX. All rights reserved.
7
8# This file is part of the CAD Exchanger software.
9
10# You may use this file under the terms of the BSD license as follows:
11
12# Redistribution and use in source and binary forms, with or without
13# modification, are permitted provided that the following conditions are met:
14# * Redistributions of source code must retain the above copyright notice,
15# this list of conditions and the following disclaimer.
16# * Redistributions in binary form must reproduce the above copyright notice,
17# this list of conditions and the following disclaimer in the documentation
18# and/or other materials provided with the distribution.
19
20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30# POSSIBILITY OF SUCH DAMAGE.
31
32
33import sys
34from pathlib import Path
35import os
36
37import cadexchanger.CadExCore as cadex
38
39sys.path.append(os.path.abspath(os.path.dirname(Path(__file__).resolve()) + r"/../../"))
40import cadex_license as license
41import math
42
43def MakeDirection(theVector: cadex.ModelData_Vector) -> cadex.ModelData_Direction:
44 aVector = theVector.Normalized()
45 return cadex.ModelData_Direction(aVector.X(), aVector.Y(), aVector.Z())
46
47def MakeCircularFace(thePos: cadex.ModelData_Point,
51 aPlane = cadex.ModelData_Plane(a3Axis)
52 aFace = cadex.ModelData_Face(aPlane)
53 aWire = cadex.ModelData_Wire(theBoundary)
54 aFace.Append(aWire)
55 return aFace
56
57def MakeCircularFaceWithInnerWire(thePos: cadex.ModelData_Point,
59 theInner: cadex.ModelData_Edge,
62 aPlane = cadex.ModelData_Plane(a3Axis)
63 aFace = cadex.ModelData_Face(aPlane)
64
65 anInner = cadex.ModelData_Wire(theInner)
66 anOuter = cadex.ModelData_Wire(theOuter)
67 aFace.Append(anInner)
68 aFace.Append(anOuter)
69 return aFace
70
71def MakeCylindricalFace(thePos: cadex.ModelData_Point,
74 theLength: float):
75 a3Axis = cadex. ModelData_Axis3Placement(thePos, theDir, cadex.ModelData_Direction.XDir())
76 aSurface = cadex.ModelData_CylindricalSurface(a3Axis, theRadius)
77 aFace = cadex.ModelData_Face(aSurface, 0, math.pi * 2, 0, theLength)
78
79 theTopEdge = None
80 theBottomEdge = None
81 # Get outer edges
82 aWire = aFace.OuterWire()
83 anIt = cadex.ModelData_Shape_Iterator(aWire, cadex.ModelData_ST_Edge)
84 while anIt.HasNext():
85 anEdge = cadex.ModelData_Edge.Cast(anIt.Next())
86 aP1 = anEdge.EndVertex().Point()
87 aP2 = anEdge.StartVertex().Point()
88 if aP1.IsEqual(aP2, cadex.ModelData_Vertex(aP2).Tolerance()):
89 # The edge is closed
90 if aP1.Z() == (theLength * theDir.Z()):
91 theTopEdge = anEdge
92 else:
93 theBottomEdge = anEdge
94
95 return aFace, theTopEdge, theBottomEdge
96
97
98def CreateNut(theRadius: float) -> cadex.ModelData_Part:
99 aNutA = theRadius * 2
100 aNutB = theRadius * 1.5
101 aHeight = theRadius / 2
102
103 anInitPosition = cadex.ModelData_Point(0.0, 0.0, -aHeight / 2)
105 a3Axis = cadex.ModelData_Axis3Placement(a2Axis)
106
107 aPlane = cadex.ModelData_Plane(a3Axis)
108 aFace = cadex.ModelData_Face(aPlane, -aNutA, aNutA, -aNutB, aNutB)
109
110 aCircle = cadex.ModelData_Circle(a2Axis, theRadius)
111 anEdge = cadex.ModelData_Edge(aCircle)
112 anInnerWire = cadex.ModelData_Wire(anEdge)
113 aFace.Append(cadex.ModelData_Wire.Cast(anInnerWire.Reversed()))
114
115 aNutShape = cadex.ModelAlgo_BRepFeatures.CreateExtrusion(aFace, cadex.ModelData_Vector(0.0, 0.0, aHeight))
116
117 # Add red color
118 aColor = cadex.ModelData_Color(255, 0, 0)
119 anApp = cadex.ModelData_Appearance(aColor)
120
121 aBRep = cadex.ModelData_BRepRepresentation(aNutShape)
122 aBRep.SetAppearance(aNutShape, anApp)
123
124 aNut = cadex.ModelData_Part(aBRep, cadex.Base_UTF16String("Nut"))
125 return aNut
126
127def CreateBolt(theRadius: float) -> cadex.ModelData_Part:
128 aMajorRadius = theRadius * 1.3
129 aLongLength = theRadius * 6
130 aShortLength = theRadius / 2
131
132 # Cylindrical faces and their edges
133
134 aFace1, aTopEdge, aMiddleOuterEdge = MakeCylindricalFace(cadex.ModelData_Point(0.0, 0.0, 0.0), cadex.ModelData_Direction.ZDir(),
135 aMajorRadius, aShortLength)
136
137 aFace2, aBottomEdge, aMiddleInnerEdge = MakeCylindricalFace(cadex.ModelData_Point(0.0, 0.0, 0.0), cadex.ModelData_Direction(0.0, 0.0, -1.0),
138 theRadius, aLongLength)
139
140 # Top circle
141 aFace3 = MakeCircularFace(cadex.ModelData_Point(0.0, 0.0, aShortLength), cadex.ModelData_Direction.ZDir(),
142 cadex.ModelData_Edge.Cast(aTopEdge.Reversed()))
143
144 # Face 4 with inner wire
145 aFace4 = MakeCircularFaceWithInnerWire(cadex.ModelData_Point(0.0, 0.0, 0.0), cadex.ModelData_Direction(0.0, 0.0, -1.0),
146 cadex.ModelData_Edge.Cast(aMiddleInnerEdge.Reversed()),
147 cadex.ModelData_Edge.Cast(aMiddleOuterEdge.Reversed()))
148
149 # Bottom circle
150 aFace5 = MakeCircularFace(cadex.ModelData_Point(0.0, 0.0, -aLongLength), cadex.ModelData_Direction(0.0, 0.0, -1.0),
151 cadex.ModelData_Edge.Cast(aBottomEdge.Reversed()))
152
153 aShell = cadex.ModelData_Shell(aFace1)
154 aShell.Append(aFace2)
155 aShell.Append(aFace3)
156 aShell.Append(aFace4)
157 aShell.Append(aFace5)
158
159 aSolid = cadex.ModelData_Solid(aShell)
160
161 # Add blue color
162 aColor = cadex.ModelData_Color(0, 0, 255)
163 anApp = cadex.ModelData_Appearance(aColor)
164
166 aBRep.SetAppearance(aSolid, anApp)
167
168 aBolt = cadex.ModelData_Part(aBRep, cadex.Base_UTF16String("Bolt"))
169 return aBolt
170
171def CreateNutBoltAssembly(theNut: cadex.ModelData_Part,
172 theBolt: cadex.ModelData_Part,
173 theDistance: float) -> cadex.ModelData_Assembly:
175 aBoltTrsf = cadex.ModelData_Transformation(cadex.ModelData_Vector(0.0, 0.0, -theDistance))
176
177 aNutBoltAssembly = cadex.ModelData_Assembly(cadex.Base_UTF16String("nut-bolt-assembly"))
178 aNutBoltAssembly.AddInstance(theNut, aNutTrsf)
179 aNutBoltAssembly.AddInstance(theBolt, aBoltTrsf)
180 return aNutBoltAssembly
181
182
183def main():
184 aKey = license.Value()
185
186 if not cadex.LicenseManager.Activate(aKey):
187 print("Failed to activate CAD Exchanger license.")
188 return 1
189
190 # Initial Parameters
191 aRadius = 0.8
192
193 aPos = [cadex.ModelData_Vector(aRadius * -2, aRadius * 1.5, 0.0),
194 cadex.ModelData_Vector(aRadius * 2, aRadius * 1.5, 0.0),
195 cadex.ModelData_Vector(aRadius * 0, aRadius * 4.5, 0.0)]
196
197 # Create shapes
198 aNut = CreateNut(aRadius)
199 aBolt = CreateBolt(aRadius)
200
201 # Create assemblies
202 aNutBoltAssembly = CreateNutBoltAssembly(aNut, aBolt, -aRadius * 4.5)
203
204 # Create a model and fill its roots
205 aModel = cadex.ModelData_Model()
206
207 # Transformations
208 for i in range(len(aPos)):
209 anAssemblyTrsf = cadex.ModelData_Transformation(aPos[i])
210 anInstance = cadex.ModelData_Instance(aNutBoltAssembly, anAssemblyTrsf, cadex.Base_UTF16String("Nut-Bolt Assembly"))
211 aModel.AddRoot(anInstance)
212
214 aWriter.Write(aModel, cadex.Base_UTF16String("out/assembly.cdx"))
215
216 print("Completed")
217 return 0
218
219if __name__ == "__main__":
220 sys.exit(main())
221
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition: Base_UTF16String.hxx:34
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
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
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
static const ModelData_Direction & ZDir()
Definition: ModelData_Direction.cxx:108
static const ModelData_Direction & XDir()
Definition: ModelData_Direction.cxx:96
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
Defines a topological face.
Definition: ModelData_Face.hxx:32
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
Writes any format that CAD Exchanger can export.
Definition: ModelData_ModelWriter.hxx:33
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
Defines a connected set of faces.
Definition: ModelData_Shell.hxx:31
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
Defines a connected set of edges.
Definition: ModelData_Wire.hxx:31
static const ModelData_Wire & Cast(const ModelData_Shape &theShape)
Definition: ModelData_Wire.cxx:88