/////////////////////////////////////////////////////////////////////////////// // Copyright (C) 2002-2025, Open Design Alliance (the "Alliance"). // All rights reserved. // // This software and its documentation and related materials are owned by // the Alliance. The software may only be incorporated into application // programs owned by members of the Alliance, subject to a signed // Membership Agreement and Supplemental Software License Agreement with the // Alliance. The structure and organization of this software are the valuable // trade secrets of the Alliance and its suppliers. The software is also // protected by copyright law and international treaty provisions. Application // programs incorporating this software must include the following statement // with their copyright notices: // // This application incorporates Open Design Alliance software pursuant to a license // agreement with Open Design Alliance. // Open Design Alliance Copyright (C) 2002-2025 by Open Design Alliance. // All rights reserved. // // By use of this software, its documentation or related materials, you // acknowledge and accept the above terms. /////////////////////////////////////////////////////////////////////////////// #include "OdaCommon.h" #include "Ed/EdUserIO.h" #include "Ed/EdCommandStack.h" #include "DbCommandContext.h" #include "DbDatabase.h" #include "RxObject.h" #include "Br/BrBrep.h" #include "Db3dSolid.h" #include "DwgStepCmds.h" #include "DwgObjectDataHelper.h" // STEP SDK includes #include "StepCore.h" #include "daiHeaderSection.h" #include "Brep2StepManifoldSolidBrepFiller.h" #include "StepAPFile.h" #include "ExStepHostAppServices.h" #include "daiObjectId.h" using namespace OdStep; OdStaticRxObject svcs; OdString g_workDir; void setModuleWorkDir(const OdString& moduleName) { g_workDir = moduleName; int pos = g_workDir.reverseFind('\\'); if (pos < 0) { pos = g_workDir.reverseFind('/'); } if (pos > 0) { g_workDir = g_workDir.mid(0, pos + 1); } } namespace { OdStepFilePtr readFile(const OdString& fileName) { OdDAI::OdHeaderSectionPtr headerSection = svcs.getHeaderSection(fileName); // // Determine schema used within a file // OdDAI::ApplicationInstancePtr fileSchema = headerSection->getEntityByType(OdDAI::kFileSchema); OdDAI::ListOfOdAnsiString* schemaIdentifiers = nullptr; fileSchema->getAttr("schema_identifiers") >> schemaIdentifiers; OdAnsiString schemaIdentifier; schemaIdentifiers->getByIndex(0) >> schemaIdentifier; OdDAI::SchemaPtr schema = oddaiFindSchema(schemaIdentifier); if (schema.isNull()) { OdAnsiString schemaFileName = svcs.findSchemaFileName(schemaIdentifier); if (schemaFileName.isEmpty()) { throw eInvalidInput; } //If schema isn't loaded, try to change working directory to $(outDir) in VS project settings OdStringArray arr; OdString s1, s2; schemaFileName = svcs.findFile(g_workDir + OD_T("Schemas/") + OdString(schemaFileName), nullptr, OdDbBaseHostAppServices::kDefault); if (schemaFileName.isEmpty()) { throw eInvalidInput; } schema = oddaiInitSchema(schemaFileName); } if (schema.isNull()) { throw eSchemaNotDefined; } OdDAI::SessionPtr pSession = oddaiCreateSession(); const char repoName[] = "StepRepo"; OdDAI::RepositoryPtr repository = pSession->createRepo(repoName); pSession->openRepo(repository); // // Read file into repository // if (repository->readFile(fileName) == eOk) { OdAPFilePtr pFile = OdAPFile::createObject(); pFile->setAppServices(&svcs); pFile->initialize(repository); return pFile; } return OdStepFilePtr(); } } //void setWorkDir(const OdString& moduleName) //{ // int pos = g_modulePath.reverseFind('\\'); // if (pos < 0) // { // pos = g_modulePath.reverseFind('/'); // } // if (pos > 0) // { // g_modulePath = g_modulePath.mid(0, pos + 1); // } //} void _StepExport3DSolid_func(OdEdCommandContext* pCmdCtx) { OdDbCommandContextPtr pDbCmdCtx(pCmdCtx); OdDbUserIO* pIO = pDbCmdCtx->dbUserIO(); OdString fileName = pIO->getString(OD_T("Enter STEP file name :")); if (fileName.isEmpty()) { pIO->putString(OD_T("Empty filename entered")); return; } fileName.replace(OdChar('\\'), OdChar('/')); if (fileName.find(OD_T(".step")) == -1 && fileName.find(OD_T(".stp")) == -1) fileName += ".step"; OdDbSelectionSetPtr pSSet = pIO->select(OD_T("Select BrBrep object to export in STEP file")); /* OdDbHandle contextId = pIO->getInt(OD_T("Enter target StepGeometricRepresentationContext id : "), 0, 0); if ((OdUInt64)contextId <= 0) { pIO->putString(OD_T("Incorrect context id")); return; } */ OdDbHandle ownerId = pIO->getInt(OD_T("Enter target StepProduct id : "), 0, 0); if ((OdUInt64)ownerId <= 0) { pIO->putString(OD_T("Incorrect context id")); return; } OdDbDatabase* pDb = pDbCmdCtx->database(); OdResult res = eSubSelectionSetEmpty; OdDbSelectionSetIteratorPtr pIter = pSSet->newIterator(); odStepInitialize(false, true); { // Step objects block. Don't remove brackets, cause after odStepUninitialize // some ifc objects destruction will be crushed. OdString newFileName = pIO->getString(OD_T("Enter new STEP file name :")); if (newFileName.isEmpty()) { pIO->putString(OD_T("Empty filename entered")); return; } newFileName.replace(OdChar('\\'), OdChar('/')); if (newFileName.find(OD_T(".step")) == -1 && newFileName.find(OD_T(".stp")) == -1) newFileName += ".step"; // // Read file into repository // OdAPFilePtr pFile = readFile(fileName); if (pFile.isNull()) { pIO->putString(OD_T("Failed to read STEP file")); return; } OdDAI::ModelPtr pModel = pFile->getModel(sdaiRW); if (pModel.isNull()) { pIO->putString(OD_T("Failed to get STEP model")); return; } /* if (!modelSchema->find("ifcadvancedbrep").isNull() || !modelSchema->find("ifctriangulatedfaceset").isNull() || !modelSchema->find("ifctriangulatedirregularnetwork").isNull()) { shapeRepresentation = pIO->getInt("1-closed shell, 2-triangulated face set, 3-triangulated irregular network, 4-polygonal"); } */ OdDAIObjectId shapeId; for (; !pIter->done();pIter->next()) { OdDb3dSolidPtr pObject = pIter->objectId().openObject(); if (pObject.isNull()) { pIO->putString(OD_T("Failed to get Brep object")); continue; } OdBrBrep brep; pObject->brep(brep); Brep2StepManifoldSolidBrepFiller brep2StepManifoldSolidBrepFiller(brep, pModel, pDb); // brep2StepManifoldSolidBrepFiller.setAsNurbs(true); brep2StepManifoldSolidBrepFiller.setBrBrepTransformation(true); brep2StepManifoldSolidBrepFiller.setBrepMaterialAndColor(/*pObject->materialId()*/DwgObjectDataHelper::getObjectMaterial(pObject), DwgObjectDataHelper::getObjectColor(pObject)); const OdDAI::SetOfOdDAIObjectId* objectsSet = pModel->getEntityExtent("mechanical_design_geometric_presentation_representation"); if (objectsSet && objectsSet->getMemberCount()) { auto it = objectsSet->createConstIterator(); it->beginning(); it->next(); OdDAIObjectId itemsContainerId; if (it->getCurrentMember() >> itemsContainerId) brep2StepManifoldSolidBrepFiller.setStyledItemsGlobalContainer(itemsContainerId); } res = brep2StepManifoldSolidBrepFiller.convertBrep2Model(shapeId); if (res) { pIO->putString(OD_T("Failed to import entity")); return; } } OdDAI::ApplicationInstancePtr pOwner = pModel->getEntityInstance(ownerId).openObject(); if (pOwner.isNull() || !(pOwner->putAttr("used_representation", shapeId))) { pIO->putString(OD_T("Failed to put shape representation")); return; } pFile->writeFile(newFileName); } odStepUninitialize(); if (res == eOk) { pIO->putString(OD_T("Exported successfully.")); } else { OdString tmp = OD_T("Export error: ") + OdError(res).description(); pIO->putString(tmp); } }