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