b311480e |
1 | // Created on: 2004-11-23 |
2 | // Created by: Pavel TELKOV |
973c2be1 |
3 | // Copyright (c) 2004-2014 OPEN CASCADE SAS |
b311480e |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
b311480e |
6 | // |
d5f74e42 |
7 | // This library is free software; you can redistribute it and/or modify it under |
8 | // the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
9 | // by the Free Software Foundation, with special exception defined in the file |
10 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
11 | // distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
b311480e |
15 | |
7fd59977 |
16 | // The original implementation Copyright: (C) RINA S.p.A |
17 | |
18 | #include <TObj_Model.hxx> |
19 | |
20 | #include <OSD_File.hxx> |
7fd59977 |
21 | #include <TDataStd_Integer.hxx> |
7fd59977 |
22 | #include <TDF_Tool.hxx> |
7fd59977 |
23 | #include <TDocStd_Owner.hxx> |
24 | |
25 | #include <TObj_Assistant.hxx> |
26 | #include <TObj_Application.hxx> |
27 | #include <TObj_CheckModel.hxx> |
28 | #include <TObj_HiddenPartition.hxx> |
29 | #include <TObj_LabelIterator.hxx> |
30 | #include <TObj_ModelIterator.hxx> |
31 | #include <TObj_Object.hxx> |
32 | #include <TObj_Partition.hxx> |
33 | #include <TObj_TObject.hxx> |
34 | #include <TObj_TModel.hxx> |
35 | #include <TObj_TNameContainer.hxx> |
36 | #include <Message_Msg.hxx> |
94708556 |
37 | #include <OSD_OpenFile.hxx> |
7fd59977 |
38 | |
25e59720 |
39 | IMPLEMENT_STANDARD_RTTIEXT(TObj_Model,Standard_Transient) |
92efcf78 |
40 | |
57c28b61 |
41 | #ifdef _MSC_VER |
7fd59977 |
42 | #include <io.h> |
43 | #else |
44 | #include <unistd.h> |
45 | #endif |
46 | |
7fd59977 |
47 | |
48 | //======================================================================= |
49 | //function : TObj_Model |
50 | //purpose : |
51 | //======================================================================= |
52 | |
53 | TObj_Model::TObj_Model () |
54 | { |
55 | myMessenger = GetApplication()->Messenger(); |
56 | } |
57 | |
58 | //======================================================================= |
59 | //function : GetApplication |
60 | //purpose : |
61 | //======================================================================= |
62 | |
63 | const Handle(TObj_Application) TObj_Model::GetApplication() |
64 | { |
65 | return TObj_Application::GetInstance(); |
66 | } |
67 | |
68 | //======================================================================= |
69 | //function : Destructor |
70 | //purpose : |
71 | //======================================================================= |
72 | |
73 | TObj_Model::~TObj_Model () |
74 | { |
75 | Close(); |
76 | } |
77 | |
78 | //======================================================================= |
79 | //function : CloseDocument |
80 | //purpose : free OCAF document |
81 | //======================================================================= |
82 | |
83 | void TObj_Model::CloseDocument (const Handle(TDocStd_Document)& theDoc) |
84 | { |
85 | // prevent Abort of the following modifs at document destruction if |
86 | // a transaction is open: see theDoc->myUndoTransaction.~() |
87 | if ( theDoc->HasOpenCommand() ) |
88 | theDoc->AbortCommand(); |
89 | |
90 | // Application |
91 | const Handle(TObj_Application) anApplication = GetApplication(); |
92 | |
7fd59977 |
93 | // just all other attributes |
94 | theDoc->Main().Root().ForgetAllAttributes(Standard_True); |
95 | anApplication->Close( theDoc ); |
96 | } |
97 | |
98 | //======================================================================= |
99 | //function : Load |
100 | //purpose : Loads the model from the file |
101 | //======================================================================= |
102 | |
be70005c |
103 | Standard_Boolean TObj_Model::Load (const TCollection_ExtendedString& theFile) |
7fd59977 |
104 | { |
105 | // Return status |
106 | Standard_Boolean aStatus = Standard_True; |
107 | |
108 | // Document |
109 | Handle(TDocStd_Document) aDoc; |
110 | |
111 | // Application |
112 | const Handle(TObj_Application) anApplication = GetApplication(); |
113 | |
114 | // Current model |
115 | const Handle(TObj_Model) me = this; |
116 | TObj_Assistant::SetCurrentModel( me ); |
117 | TObj_Assistant::ClearTypeMap(); |
118 | |
119 | Standard_Boolean isFileEmpty = checkDocumentEmpty( theFile ); |
120 | if ( isFileEmpty ) |
121 | { |
122 | // theFile is empty, create new TDocStd_Document for this model |
123 | aStatus = anApplication->CreateNewDocument(aDoc, GetFormat()); |
124 | |
125 | if ( aStatus == Standard_True ) |
126 | { |
127 | // Put model in a new attribute on root label |
128 | TDF_Label aLabel = aDoc->Main(); |
129 | Handle(TObj_TModel) anAtr = new TObj_TModel; |
130 | aLabel.AddAttribute(anAtr); |
131 | anAtr->Set( me ); |
132 | // Record that label in the model object, and initialise the new model |
133 | SetLabel(aLabel); |
134 | } |
135 | } |
136 | else |
137 | { |
138 | // retrieve TDocStd_Document from <theFile> |
1f9fb707 |
139 | Messenger()->Send(Message_Msg("TObj_M_LoadDocument") << theFile, |
7fd59977 |
140 | Message_Info); |
141 | aStatus = anApplication->LoadDocument(theFile,aDoc); |
142 | |
143 | if ( aStatus == Standard_True ) |
144 | { |
145 | // Check for validity of the model read: |
146 | // if it had wrong type, it has not been not properly restored |
147 | TDF_Label aLabel = GetLabel(); |
148 | Standard_Boolean isValid = !aLabel.IsNull() && !aDoc.IsNull(); |
149 | { |
150 | try |
151 | { |
152 | isValid = isValid && aLabel.Data() == aDoc->GetData(); |
153 | } |
a738b534 |
154 | catch (Standard_Failure const&) |
7fd59977 |
155 | { |
156 | isValid = Standard_False; |
157 | } |
158 | } |
159 | if (!isValid) |
160 | { |
161 | if (!aDoc.IsNull()) CloseDocument (aDoc); |
162 | myLabel.Nullify(); |
1f9fb707 |
163 | Messenger()->Send(Message_Msg("TObj_M_WrongFile") << theFile, |
7fd59977 |
164 | Message_Alarm); |
165 | aStatus = Standard_False; |
166 | } |
167 | } |
168 | else |
169 | { |
170 | // release document from session |
171 | // no message is needed as it has been put in anApplication->LoadDocument() |
172 | if (!aDoc.IsNull()) CloseDocument (aDoc); |
173 | myLabel.Nullify(); |
174 | } |
175 | } |
176 | // initialise the new model |
177 | if ( aStatus == Standard_True ) |
178 | { |
179 | Standard_Boolean isInitOk = Standard_False; |
180 | { |
181 | try |
182 | { |
183 | isInitOk = initNewModel(isFileEmpty); |
184 | } |
9775fa61 |
185 | catch (Standard_Failure const& anException) { |
0797d9d3 |
186 | #ifdef OCCT_DEBUG |
9775fa61 |
187 | TCollection_ExtendedString aString(anException.DynamicType()->Name()); |
188 | aString = aString + ": " + anException.GetMessageString(); |
7fd59977 |
189 | Messenger()->Send(Message_Msg("TObj_Appl_Exception") << aString); |
190 | #endif |
9775fa61 |
191 | (void)anException; |
1f9fb707 |
192 | Messenger()->Send(Message_Msg("TObj_M_WrongFile") << theFile, |
7fd59977 |
193 | Message_Alarm); |
194 | } |
195 | } |
196 | if (!isInitOk ) |
197 | { |
198 | if (!aDoc.IsNull()) CloseDocument (aDoc); |
199 | myLabel.Nullify(); |
200 | aStatus = Standard_False; |
201 | } |
202 | } |
203 | TObj_Assistant::UnSetCurrentModel(); |
204 | TObj_Assistant::ClearTypeMap(); |
205 | return aStatus; |
206 | } |
207 | |
a8d3a0b1 |
208 | //======================================================================= |
209 | //function : Load |
210 | //purpose : Load the OCAF model from a stream. If case of failure, |
211 | // it initializes the model by empty data. |
212 | //======================================================================= |
213 | |
214 | Standard_Boolean TObj_Model::Load (Standard_IStream& theIStream) |
215 | { |
216 | Handle(TDocStd_Document) aDoc; |
217 | Standard_Boolean aStatus = Standard_True, isFileLoaded = Standard_False; |
218 | const Handle(TObj_Application) anApplication = GetApplication(); |
219 | |
220 | // Current model |
221 | const Handle(TObj_Model) me = this; |
222 | TObj_Assistant::SetCurrentModel (me); |
223 | TObj_Assistant::ClearTypeMap(); |
224 | |
225 | // Retrieve TDocStd_Document from the stream. |
226 | Messenger()->Send (Message_Msg ("TObj_M_LoadDocument"), Message_Info); |
227 | aStatus = anApplication->LoadDocument (theIStream, aDoc); |
228 | if (aStatus) |
229 | { |
230 | // Check for validity of the model read: |
231 | // if it had wrong type, it has not been not properly restored |
232 | TDF_Label aLabel = GetLabel(); |
233 | Standard_Boolean isValid = (!aLabel.IsNull() && !aDoc.IsNull()); |
234 | try |
235 | { |
236 | isValid = (isValid && aLabel.Data() == aDoc->GetData()); |
237 | } |
238 | catch (Standard_Failure const&) |
239 | { |
240 | isValid = Standard_False; |
241 | } |
242 | if (!isValid) |
243 | { |
244 | if (!aDoc.IsNull()) |
245 | CloseDocument (aDoc); |
246 | myLabel.Nullify(); |
247 | Messenger()->Send (Message_Msg ("TObj_M_WrongFile"), Message_Alarm); |
248 | aStatus = Standard_False; |
249 | } |
250 | isFileLoaded = isValid; |
251 | } |
252 | else |
253 | { |
254 | // release document from session |
255 | // no message is needed as it has been put in anApplication->LoadDocument() |
256 | if (!aDoc.IsNull()) |
257 | CloseDocument (aDoc); |
258 | myLabel.Nullify(); |
259 | |
260 | aStatus = anApplication->CreateNewDocument (aDoc, GetFormat()); |
261 | if (aStatus) |
262 | { |
263 | // Put model in a new attribute on root label |
264 | TDF_Label aLabel = aDoc->Main(); |
265 | Handle(TObj_TModel) anAtr = new TObj_TModel; |
266 | aLabel.AddAttribute (anAtr); |
267 | anAtr->Set (me); |
268 | // Record that label in the model object, and initialise the new model |
269 | SetLabel (aLabel); |
270 | } |
271 | } |
272 | |
273 | // Initialise the new model |
274 | if (aStatus) |
275 | { |
276 | Standard_Boolean isInitOk = Standard_False; |
277 | try |
278 | { |
279 | isInitOk = initNewModel (!isFileLoaded); |
280 | } |
281 | catch (Standard_Failure const& anException) { |
282 | #ifdef OCCT_DEBUG |
283 | TCollection_ExtendedString aString(anException.DynamicType()->Name()); |
284 | aString = aString + ": " + anException.GetMessageString(); |
285 | Messenger()->Send (Message_Msg ("TObj_Appl_Exception") << aString); |
286 | #endif |
287 | (void) anException; |
288 | Messenger()->Send (Message_Msg ("TObj_M_WrongFile"), Message_Alarm); |
289 | } |
290 | if (!isInitOk) |
291 | { |
292 | if (!aDoc.IsNull()) |
293 | CloseDocument (aDoc); |
294 | myLabel.Nullify(); |
295 | aStatus = Standard_False; |
296 | } |
297 | } |
298 | |
299 | TObj_Assistant::UnSetCurrentModel(); |
300 | TObj_Assistant::ClearTypeMap(); |
301 | return aStatus; |
302 | } |
303 | |
7fd59977 |
304 | //======================================================================= |
305 | //function : GetFile |
306 | //purpose : Returns the full file name this model is to be saved to, |
307 | // or null if the model was not saved yet |
308 | //======================================================================= |
309 | |
1f9fb707 |
310 | Handle(TCollection_HExtendedString) TObj_Model::GetFile() const |
7fd59977 |
311 | { |
06c13a57 |
312 | Handle(TDocStd_Document) aDoc = GetDocument(); |
313 | if ( aDoc.IsNull() |
314 | || !aDoc->IsStored()) |
315 | { |
1f9fb707 |
316 | return Handle(TCollection_HExtendedString)(); |
7fd59977 |
317 | } |
06c13a57 |
318 | |
1f9fb707 |
319 | TCollection_ExtendedString aPath(aDoc->GetPath()); |
06c13a57 |
320 | return !aPath.IsEmpty() |
1f9fb707 |
321 | ? new TCollection_HExtendedString(aPath) |
322 | : Handle(TCollection_HExtendedString)(); |
7fd59977 |
323 | } |
324 | |
325 | //======================================================================= |
326 | //function : Save |
327 | //purpose : Save the model to the same file |
328 | //======================================================================= |
329 | |
330 | Standard_Boolean TObj_Model::Save () |
331 | { |
332 | Handle(TDocStd_Document) aDoc = TDocStd_Document::Get(GetLabel()); |
1f9fb707 |
333 | if (aDoc.IsNull()) |
7fd59977 |
334 | return Standard_False; |
335 | |
1f9fb707 |
336 | |
337 | if (!aDoc->GetPath().IsEmpty() ) |
338 | return SaveAs(aDoc->GetPath()); |
7fd59977 |
339 | return Standard_True; |
340 | } |
341 | |
342 | //======================================================================= |
343 | //function : SaveAs |
344 | //purpose : Save the model to a file |
345 | //======================================================================= |
346 | |
be70005c |
347 | Standard_Boolean TObj_Model::SaveAs (const TCollection_ExtendedString& theFile) |
7fd59977 |
348 | { |
349 | TObj_Assistant::ClearTypeMap(); |
350 | // OCAF document |
351 | Handle(TDocStd_Document) aDoc = TDocStd_Document::Get(GetLabel()); |
352 | if ( aDoc.IsNull() ) |
353 | return Standard_False; |
354 | |
1f9fb707 |
355 | // checking that file is present on disk is not needed because could try to save as new |
356 | // document to existent file |
7fd59977 |
357 | // checking write access permission |
94708556 |
358 | FILE *aF = OSD_OpenFile (theFile, "w"); |
7fd59977 |
359 | if (aF == NULL) { |
1f9fb707 |
360 | Messenger()->Send (Message_Msg("TObj_M_NoWriteAccess") << theFile, |
7fd59977 |
361 | Message_Alarm); |
362 | return Standard_False; |
363 | } |
364 | else |
365 | fclose (aF); |
366 | |
367 | // store transaction mode |
368 | Standard_Boolean aTrMode = aDoc->ModificationMode(); |
369 | aDoc->SetModificationMode( Standard_False ); |
370 | // store all trancienmt fields of object in OCAF document if any |
371 | Handle(TObj_ObjectIterator) anIterator; |
372 | for(anIterator = GetObjects(); anIterator->More(); anIterator->Next()) |
373 | { |
374 | Handle(TObj_Object) anOCAFObj = anIterator->Value(); |
375 | if (anOCAFObj.IsNull()) |
376 | continue; |
377 | anOCAFObj->BeforeStoring(); |
378 | } // end of for(anIterator = ...) |
379 | // set transaction mode back |
380 | aDoc->SetModificationMode( aTrMode ); |
381 | |
382 | // Application |
383 | const Handle(TObj_Application) anApplication = GetApplication(); |
384 | |
385 | // call Application->SaveAs() |
386 | Standard_Boolean aStatus = anApplication->SaveDocument (aDoc, theFile); |
387 | |
388 | TObj_Assistant::ClearTypeMap(); |
389 | return aStatus; |
390 | } |
391 | |
a8d3a0b1 |
392 | //======================================================================= |
393 | //function : SaveAs |
394 | //purpose : Save the model to a stream |
395 | //======================================================================= |
396 | |
397 | Standard_Boolean TObj_Model::SaveAs (Standard_OStream& theOStream) |
398 | { |
399 | TObj_Assistant::ClearTypeMap(); |
400 | // OCAF document |
401 | Handle(TDocStd_Document) aDoc = TDocStd_Document::Get(GetLabel()); |
402 | if (aDoc.IsNull()) |
403 | return Standard_False; |
404 | |
405 | // store transaction mode |
406 | Standard_Boolean aTrMode = aDoc->ModificationMode(); |
407 | aDoc->SetModificationMode(Standard_False); |
408 | // store all trancienmt fields of object in OCAF document if any |
409 | Handle(TObj_ObjectIterator) anIterator; |
410 | for (anIterator = GetObjects(); anIterator->More(); anIterator->Next()) |
411 | { |
412 | Handle(TObj_Object) anOCAFObj = anIterator->Value(); |
413 | if (anOCAFObj.IsNull()) |
414 | continue; |
415 | anOCAFObj->BeforeStoring(); |
416 | } // end of for(anIterator = ...) |
417 | // set transaction mode back |
418 | aDoc->SetModificationMode(aTrMode); |
419 | |
420 | // call Application->SaveAs() |
421 | Standard_Boolean aStatus = GetApplication()->SaveDocument(aDoc, theOStream); |
422 | TObj_Assistant::ClearTypeMap(); |
423 | return aStatus; |
424 | } |
425 | |
7fd59977 |
426 | //======================================================================= |
427 | //function : Close |
428 | //purpose : Close the model and free related OCAF document |
429 | //======================================================================= |
430 | |
431 | Standard_Boolean TObj_Model::Close() |
432 | { |
433 | // OCAF document |
434 | TDF_Label aLabel = GetLabel(); |
435 | if ( aLabel.IsNull() ) |
436 | return Standard_False; |
437 | Handle(TDocStd_Document) aDoc = TDocStd_Document::Get(aLabel); |
438 | if ( aDoc.IsNull() ) |
439 | return Standard_False; |
440 | |
441 | CloseDocument (aDoc); |
442 | |
443 | myLabel.Nullify(); |
444 | return Standard_True; |
445 | } |
446 | |
447 | //======================================================================= |
448 | //function : GetDocumentModel |
449 | //purpose : returns model which contains a document with the label |
450 | // returns NULL handle if label is NULL |
451 | //======================================================================= |
452 | |
453 | Handle(TObj_Model) TObj_Model::GetDocumentModel |
454 | (const TDF_Label& theLabel) |
455 | { |
456 | Handle(TObj_Model) aModel; |
457 | if(theLabel.IsNull()) |
458 | return aModel; |
459 | |
460 | Handle(TDocStd_Document) aDoc; |
461 | Handle(TDF_Data) aData = theLabel.Data(); |
462 | TDF_Label aRootL = aData->Root(); |
463 | if ( aRootL.IsNull()) |
464 | return aModel; |
465 | Handle(TDocStd_Owner) aDocOwnerAtt; |
466 | if (aRootL.FindAttribute (TDocStd_Owner::GetID(), aDocOwnerAtt)) |
467 | aDoc = aDocOwnerAtt->GetDocument(); |
468 | |
469 | if ( aDoc.IsNull() ) |
470 | return aModel; |
471 | |
472 | TDF_Label aLabel = aDoc->Main(); |
473 | Handle(TObj_TModel) anAttr; |
474 | if(aLabel.FindAttribute(TObj_TModel::GetID(), anAttr)) |
475 | aModel = anAttr->Model(); |
476 | |
477 | return aModel; |
478 | } |
479 | |
480 | //======================================================================= |
481 | //function : GetObjects |
482 | //purpose : |
483 | //======================================================================= |
484 | |
485 | Handle(TObj_ObjectIterator) TObj_Model::GetObjects() const |
486 | { |
487 | Handle(TObj_Model) me = this; |
488 | return new TObj_ModelIterator(me); |
489 | } |
490 | |
491 | //======================================================================= |
492 | //function : GetChildren |
493 | //purpose : |
494 | //======================================================================= |
495 | |
496 | Handle(TObj_ObjectIterator) TObj_Model::GetChildren() const |
497 | { |
498 | Handle(TObj_Partition) aMainPartition = GetMainPartition(); |
499 | if(aMainPartition.IsNull()) |
500 | return 0; |
501 | return aMainPartition->GetChildren(); |
502 | } |
503 | |
504 | //======================================================================= |
505 | //function : FindObject |
506 | //purpose : |
507 | //======================================================================= |
508 | |
509 | Handle(TObj_Object) TObj_Model::FindObject |
510 | (const Handle(TCollection_HExtendedString)& theName, |
511 | const Handle(TObj_TNameContainer)& theDictionary ) const |
512 | { |
513 | Handle(TObj_TNameContainer) aDictionary = theDictionary; |
514 | if ( aDictionary.IsNull() ) |
515 | aDictionary = GetDictionary(); |
516 | Handle(TObj_Object) aResult; |
517 | //Check is object with given name is present in model |
518 | if( IsRegisteredName( theName, aDictionary ) ) |
519 | { |
520 | TDF_Label aLabel = aDictionary->Get().Find( theName ); |
521 | TObj_Object::GetObj( aLabel, aResult ); |
522 | } |
523 | |
524 | return aResult; |
525 | } |
526 | |
527 | //======================================================================= |
528 | //function : GetRoot |
529 | //purpose : |
530 | //======================================================================= |
531 | |
532 | Handle(TObj_Object) TObj_Model::GetRoot() const |
533 | { |
534 | return getPartition(GetLabel()); |
535 | } |
536 | |
537 | //======================================================================= |
538 | //function : GetMainPartition |
539 | //purpose : |
540 | //======================================================================= |
541 | |
542 | Handle(TObj_Partition) TObj_Model::GetMainPartition() const |
543 | { |
544 | return getPartition( GetLabel() ); |
545 | } |
546 | |
547 | //======================================================================= |
548 | //function : SetNewName |
549 | //purpose : |
550 | //======================================================================= |
551 | |
552 | void TObj_Model::SetNewName(const Handle(TObj_Object)& theObject) |
553 | { |
554 | Handle(TObj_Partition) aPartition = TObj_Partition::GetPartition(theObject); |
555 | |
556 | //sets name if partition is found |
557 | if(aPartition.IsNull()) return; |
558 | |
559 | Handle(TCollection_HExtendedString) name = aPartition->GetNewName(); |
560 | if ( ! name.IsNull() ) theObject->SetName(name); |
561 | } |
562 | |
563 | //======================================================================= |
564 | //function : IsRegisteredName |
565 | //purpose : |
566 | //======================================================================= |
567 | |
568 | Standard_Boolean TObj_Model::IsRegisteredName(const Handle(TCollection_HExtendedString)& theName, |
569 | const Handle(TObj_TNameContainer)& theDictionary ) const |
570 | { |
571 | Handle(TObj_TNameContainer) aDictionary = theDictionary; |
572 | if ( aDictionary.IsNull() ) |
573 | aDictionary = GetDictionary(); |
574 | |
575 | if ( aDictionary.IsNull() ) |
576 | return Standard_False; |
577 | return aDictionary->IsRegistered( theName ); |
578 | } |
579 | |
580 | //======================================================================= |
581 | //function : RegisterName |
582 | //purpose : |
583 | //======================================================================= |
584 | |
585 | void TObj_Model::RegisterName(const Handle(TCollection_HExtendedString)& theName, |
586 | const TDF_Label& theLabel, |
587 | const Handle(TObj_TNameContainer)& theDictionary ) const |
588 | { |
589 | Handle(TObj_TNameContainer) aDictionary = theDictionary; |
590 | if ( aDictionary.IsNull() ) |
591 | aDictionary = GetDictionary(); |
592 | |
593 | if ( !aDictionary.IsNull() ) |
594 | aDictionary->RecordName( theName, theLabel ); |
595 | } |
596 | |
597 | //======================================================================= |
598 | //function : UnRegisterName |
599 | //purpose : |
600 | //======================================================================= |
601 | |
602 | void TObj_Model::UnRegisterName(const Handle(TCollection_HExtendedString)& theName, |
603 | const Handle(TObj_TNameContainer)& theDictionary ) const |
604 | { |
605 | Handle(TObj_TNameContainer) aDictionary = theDictionary; |
606 | if ( aDictionary.IsNull() ) |
607 | aDictionary = GetDictionary(); |
608 | |
609 | if ( !aDictionary.IsNull() ) |
610 | aDictionary->RemoveName( theName ); |
611 | } |
612 | |
613 | //======================================================================= |
614 | //function : GetDictionary |
615 | //purpose : |
616 | //======================================================================= |
617 | |
618 | Handle(TObj_TNameContainer) TObj_Model::GetDictionary() const |
619 | { |
620 | Handle(TObj_TNameContainer) A; |
621 | TDF_Label aLabel = GetLabel(); |
622 | if (!aLabel.IsNull()) |
623 | aLabel.FindAttribute(TObj_TNameContainer::GetID(),A); |
624 | return A; |
625 | } |
626 | |
627 | //======================================================================= |
628 | //function : getPartition |
629 | //purpose : |
630 | //======================================================================= |
631 | |
632 | Handle(TObj_Partition) TObj_Model::getPartition |
633 | (const TDF_Label& theLabel, |
634 | const Standard_Boolean theHidden) const |
635 | { |
636 | Handle(TObj_Partition) aPartition; |
637 | if(theLabel.IsNull()) return aPartition; |
638 | Handle(TObj_TObject) A; |
639 | |
640 | if (!theLabel.FindAttribute (TObj_TObject::GetID(), A)) |
641 | { |
642 | if (theHidden) |
643 | aPartition = new TObj_HiddenPartition(theLabel); |
644 | else |
645 | aPartition = TObj_Partition::Create(theLabel); |
646 | } |
647 | else |
648 | aPartition = Handle(TObj_Partition)::DownCast(A->Get()); |
649 | |
650 | return aPartition; |
651 | } |
652 | |
653 | //======================================================================= |
654 | //function : getPartition |
655 | //purpose : |
656 | //======================================================================= |
657 | |
658 | Handle(TObj_Partition) TObj_Model::getPartition |
659 | (const TDF_Label& theLabel, |
660 | const Standard_Integer theIndex, |
661 | const TCollection_ExtendedString& theName, |
662 | const Standard_Boolean theHidden) const |
663 | { |
664 | Handle(TObj_Partition) aPartition; |
665 | if(theLabel.IsNull()) return aPartition; |
666 | |
667 | TDF_Label aLabel = theLabel.FindChild(theIndex,Standard_False); |
668 | Standard_Boolean isNew = Standard_False; |
669 | // defining is partition new |
670 | if ( aLabel.IsNull() ) |
671 | { |
672 | aLabel = theLabel.FindChild(theIndex,Standard_True); |
673 | isNew = Standard_True; |
674 | } |
675 | // obtaining the partition |
676 | aPartition = getPartition( aLabel, theHidden ); |
677 | |
678 | //setting name to new partition |
679 | if(isNew) |
680 | aPartition->SetName(new TCollection_HExtendedString(theName)); |
681 | return aPartition; |
682 | } |
683 | |
684 | |
685 | //======================================================================= |
686 | //function : getPartition |
687 | //purpose : |
688 | //======================================================================= |
689 | |
690 | Handle(TObj_Partition) TObj_Model::getPartition |
691 | (const Standard_Integer theIndex, |
692 | const TCollection_ExtendedString& theName, |
693 | const Standard_Boolean theHidden) const |
694 | { |
695 | return getPartition (GetMainPartition()->GetChildLabel(), |
696 | theIndex, theName, theHidden); |
697 | } |
698 | |
699 | //======================================================================= |
700 | //function : initNewModel |
701 | //purpose : |
702 | //======================================================================= |
703 | |
704 | Standard_Boolean TObj_Model::initNewModel (const Standard_Boolean IsNew) |
705 | { |
706 | // set names map |
707 | TObj_TNameContainer::Set(GetLabel()); |
708 | |
709 | // do something for loaded model. |
710 | if (!IsNew) |
711 | { |
712 | // Register names of model in names map. |
713 | Handle(TObj_ObjectIterator) anIterator; |
714 | for(anIterator = GetObjects(); anIterator->More(); anIterator->Next()) |
715 | { |
716 | Handle(TObj_Object) anOCAFObj = anIterator->Value(); |
717 | if (anOCAFObj.IsNull()) |
718 | continue; |
719 | anOCAFObj->AfterRetrieval(); |
720 | } // end of for(anIterator = ...) |
721 | // update back references for loaded model by references |
722 | updateBackReferences( GetMainPartition() ); |
723 | |
724 | if ( isToCheck() ) |
725 | { |
726 | // check model consistency |
727 | Handle(TObj_CheckModel) aCheck = GetChecker(); |
728 | aCheck->Perform(); |
729 | aCheck->SendMessages(); |
730 | // tell that the model has been modified |
731 | SetModified(Standard_True); |
732 | } |
733 | } |
734 | return Standard_True; |
735 | } |
736 | |
737 | //======================================================================= |
738 | //function : updateBackReferences |
739 | //purpose : |
740 | //======================================================================= |
741 | |
742 | void TObj_Model::updateBackReferences (const Handle(TObj_Object)& theObject) |
743 | { |
744 | // recursive update back references |
745 | if ( theObject.IsNull() ) |
746 | return; |
747 | Handle(TObj_ObjectIterator) aChildren = theObject->GetChildren(); |
fc9e2095 |
748 | for(;aChildren->More(); aChildren->Next()) |
7fd59977 |
749 | { |
750 | Handle(TObj_Object) aChild = aChildren->Value(); |
751 | updateBackReferences( aChild ); |
752 | } |
753 | // update back references of reference objects |
754 | Handle(TObj_LabelIterator) anIter = |
755 | Handle(TObj_LabelIterator)::DownCast(theObject->GetReferences()); |
756 | |
757 | if(anIter.IsNull()) // to avoid exception |
758 | return; |
759 | |
760 | // LH3D15722. Remove all back references to make sure there will be no unnecessary |
761 | // duplicates, since some back references may already exist after model upgrading. |
762 | // (do not take care that object can be from other document, because |
763 | // we do not modify document, all modifications are made in transient fields) |
764 | for( ; anIter->More() ; anIter->Next()) |
765 | { |
766 | Handle(TObj_Object) anObject = anIter->Value(); |
767 | if ( !anObject.IsNull() ) |
768 | anObject->RemoveBackReference( theObject, Standard_False ); |
769 | } |
770 | // and at last create back references |
771 | anIter = Handle(TObj_LabelIterator)::DownCast(theObject->GetReferences()); |
772 | if(!anIter.IsNull()) |
773 | for( ; anIter->More() ; anIter->Next()) |
774 | { |
775 | Handle(TObj_Object) anObject = anIter->Value(); |
776 | if ( !anObject.IsNull() ) |
777 | anObject->AddBackReference( theObject ); |
778 | } |
779 | } |
780 | |
781 | //======================================================================= |
782 | //function : GetDocument |
783 | //purpose : |
784 | //======================================================================= |
785 | |
786 | Handle(TDocStd_Document) TObj_Model::GetDocument() const |
787 | { |
788 | Handle(TDocStd_Document) D; |
789 | TDF_Label aLabel = GetLabel(); |
790 | if (!aLabel.IsNull()) |
791 | D = TDocStd_Document::Get(aLabel); |
792 | return D; |
793 | } |
794 | |
795 | //======================================================================= |
796 | //function : HasOpenCommand |
797 | //purpose : |
798 | //======================================================================= |
799 | |
800 | Standard_Boolean TObj_Model::HasOpenCommand() const |
801 | { |
802 | return GetDocument()->HasOpenCommand(); |
803 | } |
804 | |
805 | //======================================================================= |
806 | //function : OpenCommand |
807 | //purpose : |
808 | //======================================================================= |
809 | |
810 | void TObj_Model::OpenCommand() const |
811 | { |
812 | GetDocument()->OpenCommand(); |
813 | } |
814 | |
815 | //======================================================================= |
816 | //function : CommitCommand |
817 | //purpose : |
818 | //======================================================================= |
819 | |
820 | void TObj_Model::CommitCommand() const |
821 | { |
822 | GetDocument()->CommitCommand(); |
823 | } |
824 | |
825 | //======================================================================= |
826 | //function : AbortCommand |
827 | //purpose : |
828 | //======================================================================= |
829 | |
830 | void TObj_Model::AbortCommand() const |
831 | { |
832 | GetDocument()->AbortCommand(); |
833 | } |
834 | |
835 | //======================================================================= |
836 | //function : IsModified |
837 | //purpose : Status of modification |
838 | //======================================================================= |
839 | |
840 | Standard_Boolean TObj_Model::IsModified () const |
841 | { |
842 | Handle(TDocStd_Document) aDoc = GetDocument(); |
843 | return aDoc.IsNull() ? Standard_False : aDoc->IsChanged(); |
844 | } |
845 | |
846 | //======================================================================= |
847 | //function : SetModified |
848 | //purpose : Status of modification |
849 | //======================================================================= |
850 | |
851 | void TObj_Model::SetModified (const Standard_Boolean theModified) |
852 | { |
853 | Handle(TDocStd_Document) aDoc = GetDocument(); |
854 | if (!aDoc.IsNull()) |
855 | { |
856 | Standard_Integer aSavedTime = aDoc->GetData()->Time(); |
857 | if (theModified) |
858 | --aSavedTime; |
859 | aDoc->SetSavedTime (aSavedTime); |
860 | } |
861 | } |
862 | |
863 | //======================================================================= |
864 | //function : checkDocumentEmpty |
865 | //purpose : Check whether the document contains the Ocaf data |
866 | //======================================================================= |
867 | |
be70005c |
868 | Standard_Boolean TObj_Model::checkDocumentEmpty (const TCollection_ExtendedString& theFile) |
7fd59977 |
869 | { |
1f9fb707 |
870 | if (theFile.IsEmpty()) |
7fd59977 |
871 | return Standard_True; |
872 | |
1f9fb707 |
873 | OSD_Path aPath (theFile); |
7fd59977 |
874 | OSD_File osdfile (aPath); |
875 | if ( !osdfile.Exists() ) |
876 | return Standard_True; |
877 | |
94708556 |
878 | FILE* f = OSD_OpenFile( theFile, "r" ); |
7fd59977 |
879 | if ( f ) |
880 | { |
881 | Standard_Boolean isZeroLengh = Standard_False; |
882 | fseek( f, 0, SEEK_END ); |
883 | if ( ftell( f ) == 0 ) |
884 | isZeroLengh = Standard_True; |
885 | |
886 | fclose( f ); |
887 | return isZeroLengh; |
888 | } |
889 | return Standard_False; |
890 | } |
891 | |
892 | //======================================================================= |
893 | //function : GetGUID |
894 | //purpose : |
895 | //======================================================================= |
896 | |
897 | Standard_GUID TObj_Model::GetGUID() const |
898 | { |
899 | Standard_GUID aGUID("3bbefb49-e618-11d4-ba38-0060b0ee18ea"); |
900 | return aGUID; |
901 | } |
902 | |
903 | //======================================================================= |
904 | //function : GetFormat |
905 | //purpose : |
906 | //======================================================================= |
907 | |
908 | TCollection_ExtendedString TObj_Model::GetFormat() const |
909 | { |
910 | return TCollection_ExtendedString ("TObjBin"); |
911 | } |
912 | |
913 | //======================================================================= |
914 | //function : GetFormatVersion |
915 | //purpose : |
916 | //======================================================================= |
917 | |
918 | Standard_Integer TObj_Model::GetFormatVersion() const |
919 | { |
920 | TDF_Label aLabel = GetDataLabel().FindChild(DataTag_FormatVersion,Standard_False); |
921 | if(aLabel.IsNull()) |
922 | return -1; |
923 | |
924 | Handle(TDataStd_Integer) aNum; |
925 | if(!aLabel.FindAttribute ( TDataStd_Integer::GetID(), aNum )) |
926 | return -1; |
927 | else |
928 | return aNum->Get(); |
929 | } |
930 | |
931 | //======================================================================= |
932 | //function : SetFormatVersion |
933 | //purpose : |
934 | //======================================================================= |
935 | |
936 | void TObj_Model::SetFormatVersion(const Standard_Integer theVersion) |
937 | { |
938 | TDF_Label aLabel = GetDataLabel().FindChild(DataTag_FormatVersion,Standard_True); |
939 | TDataStd_Integer::Set(aLabel,theVersion); |
940 | } |
941 | |
942 | |
943 | //======================================================================= |
944 | //function : GetDataLabel |
945 | //purpose : |
946 | //======================================================================= |
947 | |
948 | TDF_Label TObj_Model::GetDataLabel() const |
949 | { |
950 | return GetMainPartition()->GetDataLabel(); |
951 | } |
952 | |
953 | //======================================================================= |
954 | //function : Paste |
955 | //purpose : |
956 | //======================================================================= |
957 | |
958 | Standard_Boolean TObj_Model::Paste (Handle(TObj_Model) theModel, |
959 | Handle(TDF_RelocationTable) theRelocTable) |
960 | { |
961 | if(theModel.IsNull()) return Standard_False; |
962 | // clearing dictionary of objects names |
963 | // theModel->GetDictionary()->NewEmpty()->Paste(theModel->GetDictionary(), |
964 | // new TDF_RelocationTable); |
965 | // theModel->GetLabel().ForgetAllAttributes(Standard_True); |
966 | TObj_TNameContainer::Set(theModel->GetLabel()); |
967 | GetMainPartition()->Clone(theModel->GetLabel(), theRelocTable); |
968 | return Standard_True; |
969 | } |
970 | |
971 | //======================================================================= |
972 | //function : CopyReferences |
973 | //purpose : |
974 | //======================================================================= |
975 | |
976 | void TObj_Model::CopyReferences(const Handle(TObj_Model)& theTarget, |
977 | const Handle(TDF_RelocationTable)& theRelocTable) |
978 | { |
979 | Handle(TObj_Object) aMyRoot = GetMainPartition(); |
980 | Handle(TObj_Object) aTargetRoot = theTarget->GetMainPartition(); |
981 | aMyRoot->CopyReferences(aTargetRoot, theRelocTable); |
982 | } |
983 | |
984 | //======================================================================= |
985 | //function : GetModelName |
986 | //purpose : Returns the name of the model |
987 | // by default returns TObj |
988 | //======================================================================= |
989 | |
990 | Handle(TCollection_HExtendedString) TObj_Model::GetModelName() const |
991 | { |
992 | Handle(TCollection_HExtendedString) aName = |
993 | new TCollection_HExtendedString("TObj"); |
994 | return aName; |
995 | } |
996 | |
997 | //======================================================================= |
998 | //function : Update |
999 | //purpose : default implementation is empty |
1000 | //======================================================================= |
1001 | |
1002 | Standard_Boolean TObj_Model::Update () |
1003 | { |
1004 | return Standard_True; |
1005 | } |
1006 | |
1007 | //======================================================================= |
1008 | //function : GetChecker |
1009 | //purpose : |
1010 | //======================================================================= |
1011 | |
1012 | Handle(TObj_CheckModel) TObj_Model::GetChecker() const |
1013 | { |
1014 | return new TObj_CheckModel (this); |
1015 | } |