Files
EgtInterface/API_GdbObjects.cpp
T
Dario Sassi b0389f3fda EgtInterface 1.6b6 :
- aggiornamenti vari
- introdotto comando per merge (cucitura) di superfici
- migliorata gestione oggetti selezionati in comandi.
2015-03-11 09:06:35 +00:00

536 lines
18 KiB
C++

//----------------------------------------------------------------------------
// EgalTech 2014-2014
//----------------------------------------------------------------------------
// File : API_GdbObjects.cpp Data : 02.09.14 Versione : 1.5i1
// Contenuto : Funzioni iterazione di DB geometrico per API.
//
//
//
// Modifiche : 02.09.14 DS Creazione modulo.
//
//
//----------------------------------------------------------------------------
//--------------------------- Include ----------------------------------------
#include "stdafx.h"
#include "API.h"
#include "API_Macro.h"
#include "/EgtDev/Include/EInAPI.h"
#include "/EgtDev/Include/EGkStringUtils3d.h"
#include "/EgtDev/Include/EGnStringConverter.h"
#include "/EgtDev/Include/EgtPointerOwner.h"
using namespace std ;
//-----------------------------------------------------------------------------
BOOL
__stdcall EgtExistsObj( int nId)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, FALSE)
// verifico se esiste l'oggetto
return ( pGeomDB->ExistsObj( nId) ? TRUE : FALSE) ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtGetParent( int nId)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, GDB_ID_NULL)
// verifico se esiste l'oggetto
return pGeomDB->GetParentId( nId) ;
}
//-----------------------------------------------------------------------------
BOOL
__stdcall EgtGetGroupGlobFrame( int nId, double ptOrig[3], double vtX[3], double vtY[3], double vtZ[3])
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, FALSE)
// recupero il riferimento globale del gruppo
Frame3d frGlob ;
if ( ! pGeomDB->GetGroupGlobFrame( nId, frGlob))
return FALSE ;
// aggiorno i parametri del frame
VEC_FROM_3D( ptOrig, frGlob.Orig())
VEC_FROM_3D( vtX, frGlob.VersX())
VEC_FROM_3D( vtY, frGlob.VersY())
VEC_FROM_3D( vtZ, frGlob.VersZ())
return TRUE ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtGetGroupObjs( int nId)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, GDB_ID_NULL)
// recupero il numero di oggetti nel gruppo
return pGeomDB->GetGroupObjs( nId) ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtGetFirstInGroup( int nGroupId)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, GDB_ID_NULL)
// recupero il primo oggetto nel gruppo
return pGeomDB->GetFirstInGroup( nGroupId) ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtGetNext( int nId)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, GDB_ID_NULL)
// recupero il prossimo oggetto nello stesso gruppo
return pGeomDB->GetNext( nId) ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtGetLastInGroup( int nGroupId)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, GDB_ID_NULL)
// recupero l'ultimo oggetto nel gruppo
return pGeomDB->GetLastInGroup( nGroupId) ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtGetPrev( int nId)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, GDB_ID_NULL)
// recupero il precedente oggetto nello stesso gruppo
return pGeomDB->GetPrev( nId) ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtGetFirstGroupInGroup( int nGroupId)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, GDB_ID_NULL)
// recupero il primo gruppo nel gruppo
return pGeomDB->GetFirstGroupInGroup( nGroupId) ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtGetNextGroup( int nId)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, GDB_ID_NULL)
// recupero il prossimo gruppo nello stesso gruppo
return pGeomDB->GetNextGroup( nId) ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtGetLastGroupInGroup( int nGroupId)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, GDB_ID_NULL)
// recupero l'ultimo gruppo nel gruppo
return pGeomDB->GetLastGroupInGroup( nGroupId) ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtGetPrevGroup( int nId)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, GDB_ID_NULL)
// recupero il precedente gruppo nello stesso gruppo
return pGeomDB->GetPrevGroup( nId) ;
}
//-----------------------------------------------------------------------------
int
EgtGetFirstNameInGroup( int nGroupId, const string& sName)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, GDB_ID_NULL)
// recupero il primo oggetto nel gruppo
int nFirstId = pGeomDB->GetFirstInGroup( nGroupId) ;
string sObjName ;
if ( pGeomDB->GetName( nFirstId, sObjName) && sObjName == sName)
return nFirstId ;
return EgtGetNextName( nFirstId, sName) ;
}
//-----------------------------------------------------------------------------
int
EgtGetNextName( int nId, const string& sName)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, GDB_ID_NULL)
// recupero il prossimo oggetto nello stesso gruppo con il nome voluto
int nNextId = nId ;
while ( ( nNextId = pGeomDB->GetNext( nNextId)) != GDB_ID_NULL) {
string sObjName ;
if ( pGeomDB->GetName( nNextId, sObjName) && sObjName == sName)
return nNextId ;
}
return GDB_ID_NULL ;
}
//-----------------------------------------------------------------------------
BOOL
__stdcall EgtGetBBox( int nId, int nFlag, double ptMin[3], double ptMax[3])
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, GDB_ID_NULL)
// recupero il bounding box locale dell'oggetto
BBox3d b3Loc ;
if ( ! pGeomDB->GetLocalBBox( nId, b3Loc, nFlag))
return FALSE ;
ptMin[0] = b3Loc.GetMin().x ;
ptMin[1] = b3Loc.GetMin().y ;
ptMin[2] = b3Loc.GetMin().z ;
ptMax[0] = b3Loc.GetMax().x ;
ptMax[1] = b3Loc.GetMax().y ;
ptMax[2] = b3Loc.GetMax().z ;
return TRUE ;
}
//-----------------------------------------------------------------------------
BOOL
__stdcall EgtGetBBoxGlob( int nId, int nFlag, double ptMin[3], double ptMax[3])
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, GDB_ID_NULL)
// recupero il bounding box globale dell'oggetto
BBox3d b3Loc ;
if ( ! pGeomDB->GetGlobalBBox( nId, b3Loc, nFlag))
return FALSE ;
ptMin[0] = b3Loc.GetMin().x ;
ptMin[1] = b3Loc.GetMin().y ;
ptMin[2] = b3Loc.GetMin().z ;
ptMax[0] = b3Loc.GetMax().x ;
ptMax[1] = b3Loc.GetMax().y ;
ptMax[2] = b3Loc.GetMax().z ;
return TRUE ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtCopy( int nSouId, int nRefId, int nSonBeforeAfter)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, GDB_ID_NULL)
// eseguo la copia
int nNewId = pGeomDB->Copy( nSouId, GDB_ID_NULL, nRefId, nSonBeforeAfter) ;
EgtSetModified() ;
// se richiesto, salvo il comando Lua equivalente
if ( IsCmdLog()) {
string sLua ;
if ( nSonBeforeAfter == GDB_SON)
sLua = "EgtCopy(" + ToString( nSouId) + "," +
ToString( nRefId) + ")" +
" -- Id=" + ToString( nNewId) ;
else
sLua = "EgtCopy(" + ToString( nSouId) + "," +
ToString( nRefId) + "," +
( nSonBeforeAfter == GDB_AFTER ? "'AFTER'" : "'BEFORE'") + ")" +
" -- Id=" + ToString( nNewId) ;
LOG_INFO( GetCmdLogger(), sLua.c_str()) ;
}
// restituisco risultato
return nNewId ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtCopyGlob( int nSouId, int nRefId, int nSonBeforeAfter)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, GDB_ID_NULL)
// eseguo la copia mantenendo la posizione in globale
int nNewId = pGeomDB->CopyGlob( nSouId, GDB_ID_NULL, nRefId, nSonBeforeAfter) ;
EgtSetModified() ;
// se richiesto, salvo il comando Lua equivalente
if ( IsCmdLog()) {
string sLua ;
if ( nSonBeforeAfter == GDB_SON)
sLua = "EgtCopyGlob(" + ToString( nSouId) + "," +
ToString( nRefId) + ")" +
" -- Id=" + ToString( nNewId) ;
else
sLua = "EgtCopyGlob(" + ToString( nSouId) + "," +
ToString( nRefId) + "," +
( nSonBeforeAfter == GDB_AFTER ? "'AFTER'" : "'BEFORE'") + ")" +
" -- Id=" + ToString( nNewId) ;
LOG_INFO( GetCmdLogger(), sLua.c_str()) ;
}
// restituisco risultato
return nNewId ;
}
//-----------------------------------------------------------------------------
BOOL
__stdcall EgtRelocate( int nSouId, int nRefId, int nSonBeforeAfter)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, FALSE)
// eseguo la rilocazione
bool bOk = pGeomDB->Relocate( nSouId, nRefId, nSonBeforeAfter) ;
EgtSetModified() ;
// se richiesto, salvo il comando Lua equivalente
if ( IsCmdLog()) {
string sLua ;
if ( nSonBeforeAfter == GDB_SON)
sLua = "EgtRelocate(" + ToString( nSouId) + "," +
ToString( nRefId) + ")" +
" -- Ok=" + ToString( bOk) ;
else
sLua = "EgtRelocate(" + ToString( nSouId) + "," +
ToString( nRefId) + "," +
( nSonBeforeAfter == GDB_AFTER ? "'AFTER'" : "'BEFORE'") + ")" +
" -- Ok=" + ToString( bOk) ;
LOG_INFO( GetCmdLogger(), sLua.c_str()) ;
}
// restituisco risultato
return ( bOk ? TRUE : FALSE) ;
}
//-----------------------------------------------------------------------------
BOOL
__stdcall EgtRelocateGlob( int nSouId, int nRefId, int nSonBeforeAfter)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, FALSE)
// eseguo la rilocazione mantenendo la posizione in globale
bool bOk = pGeomDB->RelocateGlob( nSouId, nRefId, nSonBeforeAfter) ;
EgtSetModified() ;
// se richiesto, salvo il comando Lua equivalente
if ( IsCmdLog()) {
string sLua ;
if ( nSonBeforeAfter == GDB_SON)
sLua = "EgtRelocateGlob(" + ToString( nSouId) + "," +
ToString( nRefId) + ")" +
" -- Ok=" + ToString( bOk) ;
else
sLua = "EgtRelocateGlob(" + ToString( nSouId) + "," +
ToString( nRefId) + "," +
( nSonBeforeAfter == GDB_AFTER ? "'AFTER'" : "'BEFORE'") + ")" +
" -- Ok=" + ToString( bOk) ;
LOG_INFO( GetCmdLogger(), sLua.c_str()) ;
}
// restituisco risultato
return ( bOk ? TRUE : FALSE) ;
}
//-----------------------------------------------------------------------------
BOOL
__stdcall EgtChangeId( int nId, int nNewId)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, FALSE)
// eseguo cambio di Id
bool bOk = pGeomDB->ChangeId( nId, nNewId) ;
// se pezzo corrente o layer corrente
if ( bOk) {
if ( nId == EgtGetCurrPart())
EgtSetCurrPartLayer( nNewId, EgtGetCurrLayer()) ;
else if ( nId == EgtGetCurrLayer())
EgtSetCurrPartLayer( EgtGetCurrPart(), nNewId) ;
}
EgtSetModified() ;
// se richiesto, salvo il comando Lua equivalente
if ( IsCmdLog()) {
string sLua = "EgtChangeId(" + ToString( nId) + "," +
ToString( nNewId) + ")" +
" -- Ok=" + ToString( bOk) ;
LOG_INFO( GetCmdLogger(), sLua.c_str()) ;
}
return ( bOk ? TRUE : FALSE) ;
}
//-----------------------------------------------------------------------------
BOOL
__stdcall EgtErase( int nId)
{
INTVECTOR vIds ;
vIds.push_back( nId) ;
return ( EgtErase( vIds) ? TRUE : FALSE) ;
}
//-----------------------------------------------------------------------------
bool
EgtErase( const INTVECTOR& vIds)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, false)
// eseguo cancellazione
bool bOk = true ;
bool bErasedCurrPart = false ;
bool bErasedCurrLayer = false ;
int nCurrPartId = EgtGetCurrPart() ;
int nCurrLayerId = EgtGetCurrLayer() ;
for ( size_t i = 0 ; i < vIds.size() ; ++ i) {
int nId = (( vIds[i] != GDB_ID_SEL) ? vIds[i] : pGeomDB->GetFirstSelectedObj()) ;
while ( nId != GDB_ID_NULL) {
// cancello
if ( ! pGeomDB->Erase( nId))
bOk = false ;
// verifico se cancellato pezzo corrente o layer corrente
if ( nId == nCurrPartId)
bErasedCurrPart = true ;
else if ( nId == nCurrLayerId)
bErasedCurrLayer = true ;
// passo al successivo
nId = (( vIds[i] != GDB_ID_SEL) ? GDB_ID_NULL : pGeomDB->GetFirstSelectedObj()) ;
}
}
// se cancellato pezzo corrente o layer corrente
if ( bErasedCurrPart)
EgtResetCurrPartLayer() ;
else if ( bErasedCurrLayer)
EgtSetCurrPartLayer( nCurrPartId, EgtGetFirstVisibleLayer( nCurrPartId)) ;
EgtSetModified() ;
// se richiesto, salvo il comando Lua equivalente
if ( IsCmdLog()) {
string sIds ;
for ( size_t i = 0 ; i < vIds.size() ; ++ i)
sIds += ( vIds[i] != GDB_ID_SEL ? ToString( vIds[i]) : "GDB_ID_SEL") + "," ;
sIds.pop_back() ;
string sLua = "EgtErase({" + sIds + "})" +
" -- Ok=" + ToString( bOk) ;
LOG_INFO( GetCmdLogger(), sLua.c_str()) ;
}
// restituisco risultato
return bOk ;
}
//-----------------------------------------------------------------------------
BOOL
__stdcall EgtEmptyGroup( int nId)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, FALSE)
// svuoto il gruppo
bool bOk = pGeomDB->EmptyGroup( nId) ;
// se cancellato pezzo corrente o layer corrente
int nCurrPartId = EgtGetCurrPart() ;
int nCurrLayerId = EgtGetCurrLayer() ;
if ( ! pGeomDB->ExistsObj( nCurrPartId))
EgtResetCurrPartLayer() ;
else if ( ! pGeomDB->ExistsObj( nCurrLayerId))
EgtSetCurrPartLayer( nCurrPartId, EgtGetFirstVisibleLayer( nCurrPartId)) ;
EgtSetModified() ;
// se richiesto, salvo il comando Lua equivalente
if ( IsCmdLog()) {
string sLua = "EgtEmptyGroup(" + ToString( nId) + ")" +
" -- Ok=" + ToString( bOk) ;
LOG_INFO( GetCmdLogger(), sLua.c_str()) ;
}
// restituisco risultato
return ( bOk ? TRUE : FALSE) ;
}
//-----------------------------------------------------------------------------
int
__stdcall EgtGetType( int nId)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, GDB_TY_NONE)
// recupero il tipo GDB
int nType = pGeomDB->GetGdbType( nId) ;
if ( nType == GDB_TY_GEO) {
// recupero il tipo Geo
const IGeoObj* pGeoObj = pGeomDB->GetGeoObj( nId) ;
if ( pGeoObj == nullptr)
return GDB_TY_NONE ;
else
return pGeoObj->GetType() ;
}
else if ( nType == GDB_TY_GROUP)
return GDB_TY_GROUP ;
else
return GDB_TY_NONE ;
}
//-----------------------------------------------------------------------------
BOOL
__stdcall EgtGetTitle( int nId, wchar_t*& wsTitle)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, FALSE)
// recupero l'oggetto geometrico
const IGeoObj* pGeoObj = pGeomDB->GetGeoObj( nId) ;
if ( pGeoObj == nullptr)
return FALSE ;
// recupero la stringa del titolo
wsTitle = _wcsdup( stringtoW( pGeoObj->GetTitle())) ;
return (( wsTitle == nullptr) ? FALSE : TRUE) ;
}
//-----------------------------------------------------------------------------
BOOL
__stdcall EgtGroupDump( int nId, wchar_t*& wsDump)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, FALSE)
// preparo l'intestazione
string sDump = "Group " + ToString( nId) + "\r\n" ;
// preparo gli attributi
pGeomDB->DumpAttributes( nId, sDump, "\r\n") ;
// numero di nodi (figli)
int nNodes = pGeomDB->GetGroupObjs( nId) ;
sDump += "Nodes : " + ToString( nNodes) + "\r\n" ;
// riferimento in globale
Frame3d frGlob ;
if ( pGeomDB->GetGroupGlobFrame( nId, frGlob)) {
sDump += "GlobFrame :\r\n" ;
sDump += " O(" + ToString( frGlob.Orig()) + ")\r\n" ;
sDump += " X(" + ToString( frGlob.VersX()) + ")\r\n" ;
sDump += " Y(" + ToString( frGlob.VersY()) + ")\r\n" ;
sDump += " Z(" + ToString( frGlob.VersZ()) + ")\r\n" ;
}
// ingombro in globale
BBox3d b3Glob ;
if ( pGeomDB->GetGlobalBBox( nId, b3Glob, BBF_EXACT)) {
sDump += "GlobBBox :\r\n" ;
Point3d ptMin, ptMax ;
if ( b3Glob.GetMinMax( ptMin, ptMax)) {
sDump += " m(" + ToString( ptMin) + ")\r\n" ;
sDump += " M(" + ToString( ptMax) + ")\r\n" ;
}
else {
sDump += " Empty\r\n" ;
}
}
// alloco buffer di ritorno ed eseguo copia
wsDump = _wcsdup( stringtoW( sDump)) ;
return (( wsDump == nullptr) ? FALSE : TRUE) ;
}
//-----------------------------------------------------------------------------
BOOL
__stdcall EgtGeoObjDump( int nId, wchar_t*& wsDump)
{
IGeomDB* pGeomDB = GetCurrGeomDB() ;
VERIFY_GEOMDB( pGeomDB, FALSE)
// recupero l'oggetto geometrico
const IGeoObj* pGeoObj = pGeomDB->GetGeoObj( nId) ;
if ( pGeoObj == nullptr)
return FALSE ;
// preparo l'intestazione
string sDump = pGeoObj->GetTitle() + " " + ToString( nId) + "\r\n" ;
// preparo gli attributi
pGeomDB->DumpAttributes( nId, sDump, "\r\n") ;
// recupero i dati geometrici
if ( ! pGeoObj->Dump( sDump, "\r\n"))
return FALSE ;
// alloco buffer di ritorno ed eseguo copia
wsDump = _wcsdup( stringtoW( sDump)) ;
return (( wsDump == nullptr) ? FALSE : TRUE) ;
}