#include "StdAfx.h"
#include "PartsReferenceCmd.h"
#include "acm.h"
#include "acmdef.h"
#include "cauxent.h"
#include "stdmgr.h"
#include "bommgr.h"
#include "symstd.h"
#include "leader.h"
#include "symbol.h"
#include "bomstd.h"
#include "partref.h"
#include "ForCad/ForCadData.h"
#include "ExCommandATCAD.h"
#include "include\MyAES.h"
std::vector<BOMAttribute> PartsReferenceCmd::vecModelBom;
Acad::ErrorStatus postToDb(AcDbEntity* ent, AcDbObjectId& objId);
static Adesk::Boolean editStringMap(const CMapStringToString& valueMap);
PartsReferenceCmd::PartsReferenceCmd(void)
{
}
PartsReferenceCmd::~PartsReferenceCmd(void)
{
}
static void dumpStringMap(const CMapStringToString& valueMap, TCHAR *sep);
static void dumpItem(const AcDbObjectId& itemId);
void dumpItem(const AcDbObjectId& itemId)
{
CMapStringToString valueMap;
CString itemno;
Adesk::UInt32 number;
Adesk::Boolean isAutoCalc,isExpanded,isExpandable;
acutPrintf(_T("\n-------------------------\n"));
Acad::ErrorStatus es = acmBomMgr->getItemData(itemId,itemno,number,valueMap);
if( es != Acad::eOk ) return;
acutPrintf(_T("Item Number: '%s', Number of items :%d\n"),itemno,number);
dumpStringMap(valueMap,_T(", "));
es = acmBomMgr->getItemStatus(itemId,isAutoCalc,isExpanded,isExpandable);
if( es != Acad::eOk ) return;
acutPrintf(_T("\nAutoCalc : %s, Expanded : %s, Expandable :%s\n"),(isAutoCalc ? _T("Yes") : _T("No")),
(isExpanded ? _T("Yes") : _T("No")),(isExpandable ? _T("Yes") : _T("No")));
}
void dumpStringMap(const CMapStringToString& valueMap, TCHAR *sep)
{
POSITION pos = valueMap.GetStartPosition();
while( pos != NULL ) {
CString value;
CString key;
// Gets key (string) and value (pPerson)
valueMap.GetNextAssoc( pos, key,value );
acutPrintf(_T("%s : '%s'%s"),key,value,sep);
}
}
Adesk::Boolean selectBTRId(AcDbObjectId& BTRId)
{
ads_name entres;
ads_point ptres;
int res = acedEntSel(_T("\nSelect subassembly Reference <enter for main>:"), entres,ptres);
acutPrintf(_T("%s"),res);
if( res == RTNORM ) {
AcDbObjectId refId;
if( acdbGetObjectId(refId,entres) != Acad::eOk ) return Adesk::kFalse;
AcDbBlockReference * blkRef;
if( acdbOpenObject(blkRef,refId,AcDb::kForRead) != Acad::eOk) return Adesk::kFalse;
BTRId = blkRef->blockTableRecord();
blkRef->close();
}
return Adesk::kTrue;
}
Adesk::Boolean
editStringMap(CMapStringToString& valueMap)
{
POSITION pos = valueMap.GetStartPosition();
CString value, tmp, key, prompt;
while( pos != NULL ) {
// Gets key (string) and value (pPerson)
valueMap.GetNextAssoc( pos, key,value );
/*prompt.Format(_T("\nSet value for %s <%s> :"),key,value);
if( ads_getstring(1,prompt,tmp.GetBuffer(150)) == RTCAN) return Adesk::kFalse;
tmp.ReleaseBuffer();
if( tmp == _T(".") ) value = _T("");
if( tmp != _T("") ) value = tmp;*/
valueMap.SetAt(key,value);
}
return Adesk::kTrue;
}
//***************************************************
//***************************************************
//创建零件参照代码
//vecModelBom保存的是零件的Bom信息
//获取零件参照的标准Bom表
//***************************************************
//***************************************************
void PartsReferenceCmd::CretePartRef()
{
// gets a point in drawing
AcGePoint3d pnt;
int res = ads_getpoint(NULL,_T("\nSelect point :"),asDblArray(pnt));
if( res == RTCAN || res == RTERROR ) return;
// creates new instance of AcmPartRef and sets defaults
AcmPartRef *pRef = new AcmPartRef;
pRef->setSymbolDefaults(); // will create data id, because we didn't initialize it.
pRef->setOrigin(pnt);
// attempts to add partref to database
AcDbObjectId partId;
if ( postToDb(pRef, partId) != Acad::eOk) {
delete pRef;
return;
}
pRef->close();
// gets current BOM standard
CMapStringToString valueMap;
AcDbObjectId stdId;
if (acmStdMgr->getCurrent(stdId) != Acm::eOk)
return;
AcmStandard* pStandard;
if (acdbOpenObject(pStandard, stdId, AcDb::kForRead))
return;
// get BOM Standard
AcDbObjectId bomStdId;
if(pStandard->getCurrentSymbolStd(_T("AcmBOMStd"), bomStdId) != Acad::eOk){
pStandard->close();
return;
}
pStandard->close();
AcmBOMStd* pBomStd;
if (acdbOpenObject(pBomStd, bomStdId, AcDb::kForWrite))
return;
// prepares valueMap to get user's data for partref
CString numItemColumnName=_T("");
CString numItemColumnData=_T("");
Adesk::Int16 numOfItems = 1;
Adesk::Int16 len = pBomStd->columnLength();
for (int register i=0; i<len; i++)
{
const AcmCColumnDef* pColumn = ((const AcmBOMStd*)pBomStd)->column(i);
switch (pColumn->type()){
case Acm::kItem :
// Item number is saved in BOM row
break;
case Acm::kQuantity :
// number of Items
numItemColumnName = pColumn->name();
valueMap.SetAt(pColumn->name(), _T("1"));
break;
default :
{
numItemColumnName = pColumn->name();
for (std::vector<BOMAttribute>::iterator itA = vecModelBom.begin(); itA != vecModelBom.end(); ++itA)
{
if (_tcscmp(itA->InnerName.c_str(), pColumn->name()) == 0)
{
//string strTemp="";
//WStringToString(itA->Value,strTemp);
//strTemp = PartsReferenceCmd::ToString(itA->Value);
valueMap.SetAt(pColumn->name(), (LPCTSTR)(itA->Value).c_str());
}
else
{
/*string strTemp="";
WStringToString(itA->Value,strTemp);
valueMap.SetAt(itA->InnerName.c_str(), (LPCTSTR)strTemp.c_str());*/
}
}
break;
}
}
}
//valueMap.SetAt(_T("pengjiang"),_T("123"));
pBomStd->close();
// gets partref's data and assigns them to the newly created partref
CString val;
if (valueMap.Lookup(numItemColumnName, val)){
numOfItems = _ttoi(val);
}
acmBomMgr->setPartData(partId,numOfItems,valueMap);
}
Adesk::Boolean isPspace()
{
if (acdbHostApplicationServices()->workingDatabase()->tilemode())
return Adesk::kFalse;
struct resbuf cvport ;
acedGetVar(_T("CVPORT"),&cvport) ;
return (cvport.resval.rint == 1);
}
// TODO: add your other functions here
Acad::ErrorStatus postToDb(AcDbEntity* ent, AcDbObjectId& objId)
{
Acad::ErrorStatus es;
AcDbBlockTable* pBlockTable;
AcDbBlockTableRecord* pSpaceRecord;
if ((es = acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable, AcDb::kForRead)) != Acad::eOk) return es;
// Determine appropriate space from AutoCAD core function isPspace()
if (isPspace()) {
if ((es=pBlockTable->getAt(ACDB_PAPER_SPACE,pSpaceRecord,AcDb::kForWrite)) != Acad::eOk){
pBlockTable->close();
return es;
}
}
else {
if ((es = pBlockTable->getAt(ACDB_MODEL_SPACE,pSpaceRecord,AcDb::kForWrite)) != Acad::eOk){
pBlockTable->close();
return es;
}
}
if ((es = pBlockTable->close()) != Acad::eOk)
return es;
es = pSpaceRecord->appendAcDbEntity(objId, ent);
// be carefull, entity remains opened.
pSpaceRecord->close();
return es;
}
void PartsReferenceCmd::Clear()
{
vecModelBom.clear();
}