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

Refer to the Element Removal 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
42
43class RemovedSGEFinder(cadex.ModelData_Model_VoidElementVisitor):
44 #public Dictionary<ModelData_Assembly, List<ModelData_Instance>> SGEsToRemove { get set }
45
46 def __init__(self, theNameToRemove: cadex.Base_UTF16String):
47 super().__init__()
48 self.myNameToRemove = theNameToRemove
49 self.SGEsToRemove = {}
50
51 # Non-root SGEs that can be removed are under assemblies. Iterate
52 # through the assembly's child instances and find those that reference
53 # SGEs with given name
54 def VisitEnterAssembly(self, theElement: cadex.ModelData_Assembly) -> bool:
55 for aSGE in cadex.ModelData_Model_ElementIterator(theElement):
56 aChild = cadex.ModelData_Instance.Cast(aSGE)
57
58 if aChild.Reference() and aChild.Reference().Name() == self.myNameToRemove:
59 self.RemoveElement(aChild, theElement)
60
61 return True
62
63 def RemoveElement(self, theElement: cadex.ModelData_Instance, theParent: cadex.ModelData_Assembly):
64 if self.SGEsToRemove.get(theParent):
65 self.SGEsToRemove[theParent].append(theElement)
66 else:
67 self.SGEsToRemove[theParent] = [theElement]
68
69def main(theSource: str, theOutput: str, theNameToRemove: str):
70 aKey = license.Value()
71
72 if not cadex.LicenseManager.Activate(aKey):
73 print("Failed to activate CAD Exchanger license.")
74 return 1
75
76 # Load the model
77 aModel = cadex.ModelData_Model()
78
79 if not cadex.ModelData_ModelReader().Read(cadex.Base_UTF16String(theSource), aModel):
80 print("Failed to read the file " + theSource)
81 return 1
82
83 # Remove roots with specified name
84 aRootsToRemove = []
85 for anElement in cadex.ModelData_Model_ElementIterator(aModel):
86 if anElement.Name() == cadex.Base_UTF16String(theNameToRemove):
87 aRootsToRemove.append(anElement)
88
89 for aRootToRemove in aRootsToRemove:
90 aModel.RemoveRoot(aRootToRemove)
91
92 # Find the rest of scene graph elements that need to be removed and their parents
93 aFinder = RemovedSGEFinder(cadex.Base_UTF16String(theNameToRemove))
94 aModel.AcceptElementVisitor(aFinder)
95
96 # Perform the recorded removals of non-root SGEs
97 for aParent in aFinder.SGEsToRemove:
98 aChildrenToRemove = aFinder.SGEsToRemove[aParent]
99
100 for aChildToRemove in aChildrenToRemove:
101 aParent.RemoveInstance(aChildToRemove)
102
103 # Save the result
104 cadex.ModelData_ModelWriter().Write(aModel, cadex.Base_UTF16String(theOutput))
105
106 print("Completed")
107
108if __name__ == "__main__":
109 if len(sys.argv) != 4:
110 print("Usage: " + os.path.abspath(Path(__file__).resolve()) +
111 " <input_file> <output_file> <sge_to_remove>, where:")
112 print(" <input_file> is a name of the XML file to be read")
113 print(" <output_file> is a name of the CDX file where the output should be stored")
114 print(" <sge_to_remove> is a name of the scene graph elements to remove")
115 sys.exit(1)
116
117 aSource = sys.argv[1]
118 anOutput = sys.argv[2]
119 aNameToRemove = sys.argv[3]
120
121 sys.exit(main(aSource, anOutput, aNameToRemove))
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition: Base_UTF16String.hxx:34
Defines a group of scene graph element.
Definition: ModelData_Assembly.hxx:33
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