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