+ return isOk;
+}
+
+bool Model_Document::importPart(const char* theFileName,
+ std::list<std::shared_ptr<ModelAPI_Feature> >& theImported,
+ bool theCheckOnly)
+{
+ Handle(Model_Application) anApp = Model_Application::getApplication();
+ TCollection_ExtendedString aFormat;
+ if (!anApp->Format(theFileName, aFormat))
+ return false;
+
+ Handle(TDocStd_Document) aTempDoc;
+ bool isOk = loadDocument(anApp, aTempDoc, theFileName);
+
+ if (isOk && theCheckOnly) {
+ // verify all features are applicable for the current document type (e.g. PartSet)
+ std::shared_ptr<Model_Session> aSession =
+ std::dynamic_pointer_cast<Model_Session>(ModelAPI_Session::get());
+ for (TDF_ChildIterator anIt(aTempDoc->Main()); anIt.More() && isOk; anIt.Next()) {
+ TDF_Label aCurrentLab = anIt.Value();
+ Handle(TDataStd_Comment) aFeatureID;
+ TDF_Label aNewFeatuerLab;
+ if (aCurrentLab.FindAttribute(TDataStd_Comment::GetID(), aFeatureID)) {
+ TCollection_AsciiString anID(aFeatureID->Get());
+ std::string aFeatureKind(anID.ToCString());
+ if (aSession->myPlugins.find(aFeatureKind) != aSession->myPlugins.end()) {
+ std::string& aDocKind = aSession->myPlugins[aFeatureKind].second;
+ isOk = aDocKind.empty() || aDocKind == kind();
+ }
+ }
+ }
+ }
+
+ if (isOk && !theCheckOnly) {
+ // copy features from the temporary document to the current
+ Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable();
+ TDF_LabelList anAllNewFeatures;
+ // Perform the copying twice for correct references:
+ // 1. copy labels hierarchy and fill the relocation table
+ TDF_Label aMain = myDoc->Main();
+ for (TDF_ChildIterator anIt(aTempDoc->Main()); anIt.More(); anIt.Next()) {
+ TDF_Label aCurrentLab = anIt.Value();
+ Handle(TDataStd_Comment) aFeatureID;
+ TDF_Label aNewFeatuerLab;
+ if (aCurrentLab.FindAttribute(TDataStd_Comment::GetID(), aFeatureID)) {
+ TCollection_AsciiString anID(aFeatureID->Get());
+ FeaturePtr aNewFeature = addFeature(anID.ToCString());
+ std::shared_ptr<Model_Data> aData =
+ std::dynamic_pointer_cast<Model_Data>(aNewFeature->data());
+ aNewFeatuerLab = aData->label().Father();
+ Model_Tools::copyLabels(aCurrentLab, aNewFeatuerLab, aRelocTable);
+ theImported.push_back(aNewFeature);
+ }
+ anAllNewFeatures.Append(aNewFeatuerLab);
+ }
+ // 2. copy attributes
+ std::set<TCollection_AsciiString> aCoordinateLabels;
+ Model_Tools::labelsOfCoordinates(aCoordinateLabels, aRelocTable);
+ TDF_ListIteratorOfLabelList aNewIt(anAllNewFeatures);
+ for (TDF_ChildIterator anIt(aTempDoc->Main()); anIt.More(); anIt.Next()) {
+ TDF_Label aCurrentLab = anIt.Value();
+ TDF_Label aFeatureLab = aNewIt.Value();
+ if (aFeatureLab.IsNull())
+ anAllNewFeatures.Remove(aNewIt);
+ else {
+ Model_Tools::copyAttrsAndKeepRefsToCoordinates(
+ aCurrentLab, aFeatureLab, aCoordinateLabels, aRelocTable);
+ aNewIt.Next();
+ }
+ }
+
+ myObjs->synchronizeFeatures(anAllNewFeatures, true, false, false, true);
+ }
+
+ if (anApp->CanClose(aTempDoc) == CDM_CCS_OK)
+ anApp->Close(aTempDoc);
+ return isOk;
+}
+
+static bool saveDocument(Handle(Model_Application) theApp,
+ Handle(TDocStd_Document) theDoc,
+ const TCollection_ExtendedString& theFilename)
+{
+ PCDM_StoreStatus aStatus;
+ try {
+ // create the directory to save the document
+ OSD_Path aPathToFile = UTL::Path(theFilename);
+ aPathToFile.SetName("");
+ aPathToFile.SetExtension("");
+ OSD_Directory aBaseDir(aPathToFile);
+ if (aPathToFile.TrekLength() != 0 && !aBaseDir.Exists())
+ aBaseDir.Build(OSD_Protection());
+ // save the document
+ aStatus = theApp->SaveAs(theDoc, theFilename);
+ }
+ catch (Standard_Failure const& anException) {
+ Events_InfoMessage("Model_Document",
+ "Exception in saving of document: %1").arg(anException.GetMessageString()).send();
+ return false;
+ }
+ bool isDone = aStatus == PCDM_SS_OK || aStatus == PCDM_SS_No_Obj;
+ if (!isDone) {
+ switch (aStatus) {
+ case PCDM_SS_DriverFailure:
+ Events_InfoMessage("Model_Document",
+ "Can not save document: save driver-library failure").send();
+ break;
+ case PCDM_SS_WriteFailure:
+ Events_InfoMessage("Model_Document", "Can not save document: file writing failure").send();
+ break;
+ case PCDM_SS_Failure:
+ default:
+ Events_InfoMessage("Model_Document", "Can not save document").send();
+ break;
+ }
+ }
+ return isDone;