3131from nativeifc import ifc_tools
3232
3333
34- def get_export_preferences (ifcfile , preferred_context = None ):
34+ def get_export_preferences (ifcfile , preferred_context = None , create = None ):
3535 """returns a preferences dict for exportIFC.
3636 Preferred context can either indicate a ContextType like 'Model' or 'Plan',
37- or a [ContextIdentifier,ContextType] list or tuple, for ex.
38- ('Annotation','Plan'). This function will do its best to find the most
39- appropriate context."""
37+ or a [ContextIdentifier,ContextType,TargetView] list or tuple, for ex.
38+ ('Annotation','Plan') or ('Body','Model','MODEL_VIEW'). This function
39+ will do its best to find the most appropriate context. If create is True,
40+ if the exact context is not found, a new one is created"""
4041
4142 prefs = exportIFC .getPreferences ()
4243 prefs ["SCHEMA" ] = ifcfile .wrapped_data .schema_name ()
@@ -46,25 +47,80 @@ def get_export_preferences(ifcfile, preferred_context=None):
4647 cids = ifc_tools .get_body_context_ids (ifcfile )
4748 contexts = [ifcfile [i ] for i in cids ]
4849 best_context = None
50+ exact_match = False
4951 if preferred_context :
5052 if isinstance (preferred_context , str ):
5153 for context in contexts :
5254 if context .ContextType == preferred_context :
5355 best_context = context
56+ exact_match = True
5457 break
5558 elif isinstance (preferred_context , (list , tuple )):
5659 second_choice = None
5760 for context in contexts :
58- if context .ContextType == preferred_context [1 ]:
59- second_choice = context
60- if context .ContextIdentifier == preferred_context [0 ]:
61+ if len (preferred_context ) > 2 :
62+ if (context .TargetView == preferred_context [2 ]
63+ and context .ContextType == preferred_context [1 ]
64+ and context .ContextIdentifier == preferred_context [0 ]):
65+ best_context = context
66+ exact_match = True
67+ if len (preferred_context ) > 1 :
68+ if (context .ContextType == preferred_context [1 ]
69+ and context .ContextIdentifier == preferred_context [0 ]):
70+ if not exact_match :
71+ best_context = context
72+ if len (preferred_context ) == 2 :
73+ exact_match = True
74+ if context .ContextType == preferred_context [0 ]:
75+ if not exact_match :
6176 best_context = context
62- break
63- else :
64- if second_choice :
65- best_context = second_choice
66- if not best_context :
67- best_context = contexts [0 ]
77+ if len (preferred_context ) == 1 :
78+ exact_match = True
79+ if contexts :
80+ if not best_context :
81+ best_context = contexts [0 ]
82+ if create :
83+ if not exact_match :
84+ if isinstance (preferred_context , str ):
85+ best_context = ifc_tools .api_run ("context.add_context" ,
86+ ifcfile ,
87+ context_type = preferred_context )
88+ elif best_context :
89+ if len (preferred_context ) > 2 :
90+ best_context = ifc_tools .api_run ("context.add_context" ,
91+ ifcfile ,
92+ context_type = preferred_context [1 ],
93+ context_identifier = preferred_context [0 ],
94+ target_view = preferred_context [2 ],
95+ parent = best_context )
96+ elif len (preferred_context ) > 1 :
97+ best_context = ifc_tools .api_run ("context.add_context" ,
98+ ifcfile ,
99+ context_type = preferred_context [1 ],
100+ context_identifier = preferred_context [0 ],
101+ parent = best_context )
102+ else :
103+ if len (preferred_context ) > 1 :
104+ best_context = ifc_tools .api_run ("context.add_context" ,
105+ ifcfile ,
106+ context_type = preferred_context [1 ])
107+ if len (preferred_context ) > 2 :
108+ best_context = ifc_tools .api_run ("context.add_context" ,
109+ ifcfile ,
110+ context_type = preferred_context [1 ],
111+ context_identifier = preferred_context [0 ],
112+ target_view = preferred_context [2 ],
113+ parent = best_context )
114+ else :
115+ best_context = ifc_tools .api_run ("context.add_context" ,
116+ ifcfile ,
117+ context_type = preferred_context [1 ],
118+ context_identifier = preferred_context [0 ],
119+ parent = best_context )
120+ else :
121+ best_context = ifc_tools .api_run ("context.add_context" ,
122+ ifcfile ,
123+ context_type = preferred_context [0 ])
68124 return prefs , best_context
69125
70126
@@ -221,7 +277,11 @@ def create_annotation(obj, ifcfile):
221277 exportIFC .curvestyles = {}
222278 exportIFC .ifcopenshell = ifcopenshell
223279 exportIFC .ifcbin = exportIFCHelper .recycler (ifcfile , template = False )
224- prefs , context = get_export_preferences (ifcfile , preferred_context = "Plan" )
280+ if is_annotation (obj ) and Draft .getType (obj ) != "SectionPlane" :
281+ context_type = "Plan"
282+ else :
283+ context_type = "Model"
284+ prefs , context = get_export_preferences (ifcfile , preferred_context = context_type , create = True )
225285 prefs ["BBIMDIMS" ] = True # Save dimensions as 2-point polylines
226286 history = get_history (ifcfile )
227287 # TODO The following prints each edge as a separate IfcGeometricCurveSet
0 commit comments