报告一下我的学习成果
我用ObjectARX Project写了如下的代码
实现的功能是在当前绘图中pick点,将点坐标存在SQL Server数据库中的同时将pick的点顺序连线
此代码编译通过且加载ARX后实现预期功能
但在关闭AutoCAD时,无论选择是否保存都会导致AutoCAD非正常退出(保存能成功)
请帮我看看这段程序的bug在哪里,帮我完善一下,谢谢
#include "StdAfx.h"
CString xpt[100]; CString ypt[100]; float xco[100]; float yco[100]; //----------------------------------------------------------------------------- #define szRDS _RXST("lemon")
//----------------------------------------------------------------------------- //----- ObjectARX EntryPoint class CPickPointApp : public AcRxArxApp {
public: CPickPointApp () : AcRxArxApp () {}
virtual AcRx::AppRetCode On_kInitAppMsg (void *pkt) { AcRx::AppRetCode retCode =AcRxArxApp::On_kInitAppMsg (pkt) ; // TODO: Add your initialization code here
return (retCode) ; }
virtual AcRx::AppRetCode On_kUnloadAppMsg (void *pkt) { AcRx::AppRetCode retCode =AcRxArxApp::On_kUnloadAppMsg (pkt) ; // TODO: Add your code here
return (retCode) ; }
virtual void RegisterServerComponents () { }
// ----- lemonPickPoint.pp command (do not rename) static void lemonPickPointpp(void) { // Add your code for command lemonPickPoint.pp here ads_point pt; int NodeNumCount; //定义节点数,即最大的节点编号数值 acedGetInt("Please input the node number:",&NodeNumCount); for(int i=0;i<NodeNumCount;i++) { if(acedGetPoint(NULL,"\nPick a point:",pt)==RTNORM) { xpt.Format("%g",pt[X]); ypt.Format("%g",pt[Y]); acutPrintf("\nThe coordinates of the point is:%s,%s",xpt,ypt); } }
_ConnectionPtr m_pConnection; AfxOleInit(); HRESULT hr; hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象 if(SUCCEEDED(hr)) { hr = m_pConnection->Open("Provider=SQLOLEDB;Data Source=CZQDAY;User ID=saassword=820306;Initial Catalog=TOSHIBA1","","",adModeUnknown);///连接数据库 } try { _variant_t varProjectID,varID,varx,vary; for(int i=0;i<NodeNumCount;i++) { atof(xpt); atof(ypt); varProjectID = "as001"; varID = i+1; varx = xpt; vary = ypt; _RecordsetPtr t_pRecordset; t_pRecordset.CreateInstance("ADODB.Recordset"); t_pRecordset->Open("SELECT * FROM xy",_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText); t_pRecordset->MoveFirst(); t_pRecordset->AddNew(); t_pRecordset->utCollect("ProjectID",varProjectID); t_pRecordset->utCollect("id",varID); t_pRecordset->utCollect("x",varx); t_pRecordset->utCollect("y",vary); t_pRecordset->Update();//只有写了这句才能刷新数据库!! //Refresh();//只有运行这个函数,才能保存成功 t_pRecordset->Close(); } } catch(_com_error e)///捕捉异常 { AfxMessageBox("保存不成功!");///显示错误信息 }
//更改数据类型,CString->float for (int j=0;j<NodeNumCount;j++) { xco[j]=atof(xpt[j]); yco[j]=atof(ypt[j]); }
//pick点,连续画直线 AcDbBlockTableRecord *pBlkTableRcd =new AcDbBlockTableRecord();//创建块表记录 //以写方式得到当前块表的指针 AcDbBlockTable *pBlkTable=NULL; acdbHostApplicationServices()->workingDatabase() ->getSymbolTable(pBlkTable, AcDb::kForWrite); //将新的块表记录加入块表中,并获得块表记录的ID AcDbObjectId Id; pBlkTable->add(Id, pBlkTableRcd); pBlkTable->close();
for(int k=0;k<NodeNumCount;k++) { //生成直线 AcGePoint3d startPt(xco[k],yco[k],0.0);//定义直线起点 AcGePoint3d endPt(xco[k+1],yco[k+1],0.0);//定义直线终点 AcDbLine *pLine=new AcDbLine(startPt,endPt); AcDbBlockTable *pBlkTable2=NULL; acdbHostApplicationServices()->workingDatabase() ->getSymbolTable(pBlkTable2, AcDb::kForRead);
pBlkTable2->getAt(ACDB_MODEL_SPACE, pBlkTableRcd, AcDb::kForWrite); pBlkTable2->close();
AcDbObjectId lineId; pBlkTableRcd->appendAcDbEntity(lineId, pLine); pLine->setLayer("Layer_Frame");//设置创建轮廓线的层为"Layer_Frame" pLine->close(); } pBlkTableRcd->close();
}
} ;
//----------------------------------------------------------------------------- IMPLEMENT_ARX_ENTRYPOINT(CPickPointApp)
ACED_ARXCOMMAND_ENTRY_AUTO(CPickPointApp, lemonPickPoint, pp, pp, ACRX_CMD_TRANSPARENT, NULL) |