/////////////////////////////////////////////////////////////////////////////// // 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 "ExMeshRayIntersection.h" #include "StaticRxObject.h" #include "DbUserIO.h" #include "DbCommandContext.h" #include "DbBlockTableRecord.h" #include "DbLine.h" #include "DbPolyline.h" #include "Db2dPolyline.h" #include "Db3dPolyline.h" #include "DbPoint.h" #include "DbRay.h" class SurfaceFilter : public OdStaticRxObject { public: SurfaceFilter() {} int addDrawables(OdGsView*) { return 0; } void removeDrawables(OdGsView*) { } bool check(const OdDbObjectId&) { return true; } bool append(const OdDbObjectId& entId, const OdDbSelectionMethod*) { return check(entId); } bool remove(const OdDbObjectId& entId, const OdDbSelectionMethod*) { return check(entId); } }; const OdString MeshRayIntersectCommand::globalName() const { return OD_T("MeshRayIntersect"); } const OdString MeshRayIntersectCommand::groupName() const { return OD_T("MODELER GEOMETRY"); } void MeshRayIntersectCommand::execute(OdEdCommandContext* pCmdCtx) { OdDbCommandContextPtr pDbCmdCtx(pCmdCtx); OdDbDatabasePtr pDb = pDbCmdCtx->database(); OdSmartPtr pIO = pDbCmdCtx->userIO(); if (pIO.isNull()) return; try { SurfaceFilter filter; OdDbSelectionSetPtr pRaySelection = pIO->select(OD_T("Select Ray(Linear entity) for intersection:"), OdEd::kSelSingleEntity, 0, OdString::kEmpty, &filter); if (pRaySelection->numEntities() == 0) { pIO->putString(OD_T("Ray is not selected.")); return; } OdDbSelectionSetPtr pMeshSelection = pIO->select(OD_T("Select Mesh for intersection:"), OdEd::kSelSingleEntity, 0, OdString::kEmpty, &filter); if (pMeshSelection->numEntities() == 0) { pIO->putString(OD_T("Mesh is not selected.")); return; } OdGePoint3d rayStart; OdGeVector3d rayDirectin; OdDbObjectPtr pObjLinear = pRaySelection->objectIdArray().first().safeOpenObject(OdDb::kForWrite); OdDbEntityPtr pLinearEnt = OdDbEntity::cast(pObjLinear.get()); if (pLinearEnt.isNull()) return; if (pLinearEnt->isKindOf(OdDbLine::desc())) { OdDbLinePtr pLine = OdDbLine::cast(pLinearEnt); rayStart = pLine->startPoint(); rayDirectin = pLine->endPoint() - rayStart; } else if (pLinearEnt->isKindOf(OdDbPolyline::desc())) { OdDbPolylinePtr pPolyLine = OdDbPolyline::cast(pLinearEnt); if (pPolyLine->numVerts() != 2) { OdGePoint3d rayEnd; pPolyLine->getStartPoint(rayStart); pPolyLine->getEndPoint(rayEnd); rayDirectin = rayEnd - rayStart; } else { pIO->putString(OD_T("Polyline has to much vertecies.")); return; } } else if (pLinearEnt->isKindOf(OdDb3dPolyline::desc())) { OdDb3dPolylinePtr p3dPolyLine = OdDb3dPolyline::cast(pLinearEnt); OdGePoint3d rayEnd; p3dPolyLine->getStartPoint(rayStart); p3dPolyLine->getEndPoint(rayEnd); rayDirectin = rayEnd - rayStart; } else if (pLinearEnt->isKindOf(OdDb2dPolyline::desc())) { OdDb2dPolylinePtr p2dPolyLine = OdDb2dPolyline::cast(pLinearEnt); OdGePoint3d rayEnd; p2dPolyLine->getStartPoint(rayStart); p2dPolyLine->getEndPoint(rayEnd); rayDirectin = rayEnd - rayStart; } else if (pLinearEnt->isKindOf(OdDbRay::desc())) { OdDbRayPtr pRay = OdDbRay::cast(pLinearEnt); rayStart = pRay->basePoint(); rayDirectin = pRay->unitDir(); } else { pIO->putString(OD_T("Unsupported Ray entity")); return; } OdDbSelectionSetIteratorPtr pIt = pMeshSelection->newIterator(); int numInterections = 0; while (!pIt->done()) { OdDbObjectPtr pObj = pIt->objectId().safeOpenObject(OdDb::kForWrite); OdDbEntityPtr pSubDMesh = OdDbEntity::cast(pObj); if (pSubDMesh.isNull() || !pSubDMesh->isKindOf(OdDbSubDMesh::desc())) { pIO->putString(OD_T("Unsupported entity.")); return; } OdArray subEnts; OdArray retDistances; OdGePoint3dArray retIntersectionPnt; OdResult res = static_cast(pSubDMesh.get())->computeRayIntersection(rayStart, rayDirectin, subEnts, retDistances, retIntersectionPnt); if (res != eOk) { OdString tmp; tmp.format(OD_T("Error : %s"), OdError(res).description().c_str()); pIO->putString(tmp); return; } OdDbObjectId ownerId = pSubDMesh->ownerId(); if (ownerId && ownerId.database()) { OdDbBlockTableRecordPtr pBlock = ownerId.openObject(OdDb::kForWrite); numInterections += retIntersectionPnt.size(); OdGeExtents3d extens; static_cast(pSubDMesh.get())->getGeomExtents(extens); double extensDistance = extens.maxPoint().distanceTo(extens.minPoint()); for (auto intersectPnt : retIntersectionPnt) { OdDb3dSolidPtr pSolid3d = OdDb3dSolid::createObject(); pSolid3d->createSphere(extensDistance / 190); OdGeMatrix3d matr; matr.setCoordSystem(intersectPnt, OdGeVector3d::kXAxis, OdGeVector3d::kYAxis, OdGeVector3d::kZAxis); pSolid3d->transformBy(matr); OdCmColor cmEnt; cmEnt.setRGB(255, 0, 0); pSolid3d->setColor(cmEnt); pBlock->appendOdDbEntity(pSolid3d); } } pIt->next(); } OdString tmp; tmp.format(OD_T("%d intersection point were found"), numInterections); pIO->putString(tmp); } catch (...) { pIO->putString(OD_T("Create Surface Failed!")); } }