Hide menu
Loading...
Searching...
No Matches
meshing/remeshing/remeshing.py

Refer to the Remeshing 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
41
42import typing
43
44class MeshReplacementVisitor(cadex.ModelData_Model_VoidElementVisitor):
45 def __init__(self):
46 super().__init__()
47 self.myRootReplacements: typing.Dict[cadex.ModelData_Part, cadex.ModelData_Part] = {}
48 self.myInstances: typing.Deque[cadex.ModelData_Instance] = []
49 self.myReplacedParts: typing.Dict[cadex.ModelData_Part, cadex.ModelData_Part] = {}
50
51 def VisitEnterInstance(self, theInstance: cadex.ModelData_Instance):
52 self.myInstances.append(theInstance)
53 return True
54
55 def VisitLeaveInstance(self, theInstance: cadex.ModelData_Instance):
56 self.myInstances.pop()
57
58 def VisitPart(self, thePart: cadex.ModelData_Part):
59 if thePart.BRepRepresentation().IsNull():
60 return
61
62 if self.myReplacedParts.get(thePart) and len(self.myInstances) > 0:
63 self.myInstances[-1].SetReference(self.myReplacedParts[thePart])
64 return
65
66 aNewPart = cadex.ModelData_Part(thePart.BRepRepresentation(), thePart.Name())
67 aNewPart.SetAppearance(thePart.Appearance())
68 aNewPart.AddProperties(thePart.Properties())
69 aNewPart.AddPMI(thePart.PMI())
70 for anIt in thePart.GetLayerIterator():
71 aNewPart.AddToLayer(anIt)
72
73 aMesherParams = cadex.ModelAlgo_BRepMesherParameters(cadex.ModelAlgo_BRepMesherParameters.Fine)
74 aMesher = cadex.ModelAlgo_BRepMesher(aMesherParams)
75 aMesher.Compute(aNewPart)
76
77 if len(self.myInstances) == 0:
78 self.myRootReplacements[thePart] = aNewPart
79 else:
80 self.myRootReplacements[thePart] = aNewPart
81 self.myInstances[-1].SetReference(aNewPart)
82
83
84def main(theSource: str, theDest: str):
85 aKey = license.Value()
86
87 if not cadex.LicenseManager.Activate(aKey):
88 print("Failed to activate CAD Exchanger license.")
89 return 1
90
92 aModel = cadex.ModelData_Model()
93
94 print("Conversion started...")
95 # Opening and converting the file
96 if not aReader.Read(cadex.Base_UTF16String(theSource), aModel):
97 print("Failed to open and convert the file " + theSource)
98 return 1
99
100 aVisitor = MeshReplacementVisitor()
101 aModel.AcceptElementVisitor(aVisitor)
102 aNewRoots: typing.List[cadex.ModelData_SceneGraphElement] = []
103 for aRoot in aModel.GetElementIterator():
104 if aRoot.TypeId() == cadex.ModelData_Part.GetTypeId():
105 aRootPart = cadex.ModelData_Part.Cast(aRoot)
106 assert aVisitor.myRootReplacements.get(aRootPart), "Part was not processed"
107 aNewRoots.append(aVisitor.myRootReplacements[aRootPart])
108 else:
109 aNewRoots.append(aRoot)
110
111 aModel.Clear()
112 for aNewRoot in aNewRoots:
113 aModel.AddRoot(aNewRoot)
114
116 # Converting and writing the model to file
117 if not aWriter.Write(aModel, cadex.Base_UTF16String(theDest)):
118 print("Failed to convert and write the file to specified format " + theDest)
119 return 1
120
121 print("Completed")
122 return 0
123
124if __name__ == "__main__":
125 if len(sys.argv) != 3:
126 print(" <input_file> is a name of the SLD file to be read")
127 print(" <output_file> is a name of the VRML file to Save() the model")
128 sys.exit(1)
129
130 aSource = os.path.abspath(sys.argv[1])
131 aDest = os.path.abspath(sys.argv[2])
132
133 sys.exit(main(aSource, aDest))
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition: Base_UTF16String.hxx:34
Computes a polygonal representation from a B-Rep one.
Definition: ModelAlgo_BRepMesher.hxx:48
Defines parameters used by the B-Rep mesher.
Definition: ModelAlgo_BRepMesherParameters.hxx:33
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
Reads any format that CAD Exchanger can import.
Definition: ModelData_ModelReader.hxx:33
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
static IdType GetTypeId()
Definition: ModelData_Part.cxx:270
Base class for part, instance and assembly.
Definition: ModelData_SceneGraphElement.hxx:39