//获取文件名
int
getFile(
// input:
const char* pCmdAsk, // prompt for command line mode
const char* pDiaAsk, // prompt for dialog box mode
const char* pExt, // file extension
// output:
char* pResult) // selected file returned to the caller.
// must be an allocated buffer!
{
char tmpResult[134];
struct resbuf *pRb = NULL;
int res = RTERROR;
char drive[_MAX_DRIVE], dir[_MAX_DIR];
char fname[_MAX_FNAME], ext[_MAX_EXT];
// command line argument?
//
if ( NULL != (pRb = acedGetArgs()) && RTSTR == pRb->restype) {
// yes!
//
strcpy( tmpResult, pRb->resval.rstring );
// continue later: look if acedFindFile finds the file!
//
res = RTNORM;
}
else {
// no command line argument
//
struct resbuf fileDia;
// is "filedia" set to 1?
//
acedGetVar( "filedia", &fileDia );
if (1 == fileDia.resval.rint) {
// yes, display dialog box
//
struct resbuf fileRes;
res = acedGetFileD( pDiaAsk, NULL, pExt, 0, &fileRes );
if (RTNORM != res) return res;
if (RTSTR == fileRes.restype) {
// user selected valid file
//
strcpy( pResult, fileRes.resval.rstring );
free( fileRes.resval.rstring );
return RTNORM;
}
}
// Prompt at command line required. Either
// filedia = 0, or "type it" button in dialog was pressed.
//
res = acedGetString( 0, pCmdAsk, tmpResult );
if (RTNORM != res) return res;
}
// look if there's already the file extension attached
//
_splitpath( tmpResult, drive, dir, fname, ext );
// if not, append file extension
//
if (stricmp( ext, pExt ) != 0)
_makepath( tmpResult, drive, dir, fname, pExt );
// look if file exists (only if not in dialog mode)
//
res = acedFindFile( tmpResult, pResult );
if (RTNORM != res)
acutPrintf( "/nUnable to open %s./n", tmpResult );
return res;
}
例子: char dwgName[_MAX_PATH];
if (RTNORM != getFile( "Enter DWG name", "Select Drawing", "dwg",
dwgName ))
{
return;
}
——————————————————————————————————————————————
//若X坐标或Y坐标重合,判为意外,不进行SetView操作
if ((fabs(Pt1.x-Pt2.x)<1e-6)||(fabs(Pt1.y-Pt2.y)<1e-6))
return;
//确保两个坐标点分别为左上角和右下角
if (Pt1.x>Pt2.x) {
double tmp;
tmp = Pt1.x;
Pt1.x = Pt2.x;
Pt2.x = tmp;
}
if (Pt2.y>Pt1.y) {
double tmp;
tmp = Pt1.y;
Pt1.y = Pt2.y;
Pt2.y = tmp;
}
//获取当前DwgView的尺寸
CRect CADrect;
acedGetAcadDwgView()->GetClientRect(&CADrect);
double width,height,ratio;
ratio = (double)(CADrect.right-CADrect.left)/(double)(CADrect.bottom-CADrect.top);
if (fabs(ratio)<1e-6)
return;
if ((Pt2.x-Pt1.x)/(Pt1.y-Pt2.y) > ratio) {
width = Pt2.x-Pt1.x;
height = width/ratio;
}else{
height = Pt1.y-Pt2.y;
width = height * ratio;
}
//设置当前视图中心点
CenterPt.x = (Pt1.x+Pt2.x)/2;
CenterPt.y = (Pt1.y+Pt2.y)/2;
//改变当前视图
AcDbViewTableRecord pVwRec;
pVwRec.setCenterPoint(CenterPt);
pVwRec.setWidth(width * ex_ratio);
pVwRec.setHeight(height * ex_ratio);
acedSetCurrentView( &pVwRec, NULL );
}
///////////////////////////////////////////////////////////////
// 函 数 名 : oxaGetVar
// 函数功能 :
// 处理过程 :
// 备 注 :
// 作 者 : user
// 时 间 : 2004年6月16日
// 返 回 值 : int
// 参数说明 : const CString strSym,
// AcGePoint3d &vOut
///////////////////////////////////////////////////////////////
int oxaGetVar(const CString strSym, AcGePoint3d &vOut )
{
resbuf rbVar ;
int iRt=acedGetVar(strSym, &rbVar) ;
if (iRt!=RTNORM)
{
return iRt;
}
//oxaPrint(&rbVar);
if (rbVar.restype==RTPOINT)
{
vOut.x=rbVar.resval.rpoint[0];
vOut.y=rbVar.resval.rpoint[1];
}
if (rbVar.restype==RT3DPOINT)
{
vOut.x=rbVar.resval.rpoint[0];
vOut.y=rbVar.resval.rpoint[1];
vOut.z=rbVar.resval.rpoint[2];
}
return RTNORM;
}
/////////////////////////////////////////////////////////////////////////////////
//# DOC.BEGIN
//# 函数名称: oxaGetVar
//# 函数编号: OXA
//# 函数声明:
//# 函数参数: const CString strSym,
// int &vOut
//# 返回值: int
//# 函数分类:
//# 函数功能: 获取系统变量, 封装acedGetVar()
//# 注意事项:
//# 涉及的全局变量:
//# 调用的OXARX函数:
//# 函数算法:
//# ACAD版本:R14 R15 R16
//# 配合函数:
//# 类似函数:
//# 替换函数:
//# 现存缺陷:
//# 示例程序:
//# 测试要求:
//# 历史记录: 2003年11月10日 , zjw ,完成
//
//# DOC.END
//////////////////////////////////////////////////////////////////////////
int oxaGetVar(const CString strSym, int &vOut )
{
resbuf rbVar;
int iRt=acedGetVar(strSym, &rbVar) ;
if (iRt!=RTNORM)
{
return iRt;
}
if (rbVar.restype==RTLONG)
{
vOut=rbVar.resval.rlong;
}
if (rbVar.restype==RTSHORT)
{
vOut=rbVar.resval.rint;
}
return RTNORM;
}
/////////////////////////////////////////////////////////////////////////////////
//# DOC.BEGIN
//# 函数名称: oxaGetVar
//# 函数编号: OXA
//# 函数声明:
//# 函数参数: const CString strSym,
// double &vOut
//# 返回值: int
//# 函数分类:
//# 函数功能: 获取系统变量, 封装acedGetVar()
//# 注意事项:
//# 涉及的全局变量:
//# 调用的OXARX函数:
//# 函数算法:
//# ACAD版本:R14 R15 R16
//# 配合函数:
//# 类似函数:
//# 替换函数:
//# 现存缺陷:
//# 示例程序:
//# 测试要求:
//# 历史记录: 2003年11月24日 , zjw ,完成
//
//# DOC.END
int oxaGetVar(const CString strSym, double &vOut )
{
resbuf rbVar;
int iRt=acedGetVar(strSym, &rbVar) ;
if (iRt!=RTNORM)
{
return iRt;
}
if (rbVar.restype==RTREAL)
{
vOut=rbVar.resval.rreal;
}
return RTNORM;
}
/////////////////////////////////////////////////////////////////////////////////
//# DOC.BEGIN
//# 函数名称: oxaGetVar
//# 函数编号: OXA
//# 函数声明:
//# 函数参数: const CString strSym,
// CString &vOut
//# 返回值: int
//# 函数分类:
//# 函数功能:获取系统变量, 封装acedGetVar()
//# 注意事项:
//# 涉及的全局变量:
//# 调用的OXARX函数:
//# 函数算法:
//# ACAD版本:R14 R15 R16
//# 配合函数:
//# 类似函数:
//# 替换函数:
//# 现存缺陷:
//# 示例程序:
//# 测试要求:
//# 历史记录: 2003年11月24日 , zjw ,完成
//
//# DOC.END
int oxaGetVar(const CString strSym, CString &vOut )
{
resbuf rbVar;
int iRt=acedGetVar(strSym, &rbVar) ;
if (iRt!=RTNORM)
{
return iRt;
}
if (rbVar.restype==RTSTR)
{
vOut=rbVar.resval.rstring;
}
return RTNORM;
}
// 函数名 : SetCurTextStyle
// 描述 : 设置当前TextStyle
// 返回 : Acad::ErrorStatus
// 参数 : const char* lpStyleName
// 参数 : AcDbDatabase* pDb/* = NULL */
Acad::ErrorStatus SetCurTextStyle(const char* lpStyleName, AcDbDatabase* pDb/* = NULL */)
{
AcDbDatabase* pCurDb = pDb;
if (pCurDb == NULL)
pCurDb = acdbHostApplicationServices()->workingDatabase();
AcDbTextStyleTableRecordPointer spRecord(lpStyleName, pCurDb, AcDb::kForRead);
Acad::ErrorStatus es = spRecord.openStatus();
if (es == Acad::eOk)
{
es = pCurDb->setTextstyle(spRecord->objectId());
}
return es;
}
// Function name : SetCurLayer
// Descrīption : 设置当前层
// Return type : Acad::ErrorStatus
// Argument : const char* lpLayerName
// Argument : AcDbDatabase* pDb/* = NULL */
Acad::ErrorStatus SetCurLayer(const char* lpLayerName, AcDbDatabase* pDb/* = NULL */)
{
AcDbDatabase* pCurDb = pDb;
if (pCurDb == NULL)
pCurDb = acdbHostApplicationServices()->workingDatabase();
AcDbLayerTableRecordPointer spRecord(lpLayerName, pCurDb, AcDb::kForRead);
Acad::ErrorStatus es = spRecord.openStatus();
if (es == Acad::eOk)
{
es = pCurDb->setClayer(spRecord->objectId());
}
return es;
}
//获取属性块中所有属性的字符串值,并且存于链表中
CODE:
void FEGroups::iterateDictionary()
{
//obtain the GROUP dictionary by looking up "ACAD_GROUP" in the named object dictionary
//
/* AcDbDictionary *pNamedobj;
acdbHostApplicationServices()->workingDatabase()
->getNamedObjectsDictionary(pNamedobj, AcDb::kForRead);
// Get a pointer to the ASDK_DICT dictionary.
//
AcDbDictionary *pDict;
pNamedobj->getAt("ACAD_GROUP", (AcDbObject*&)pDict,
AcDb::kForRead);
pNamedobj->close();
*/
// Get a pointer to the ACAD_GROUP dictionary
AcDbDictionary *pDict;
acdbHostApplicationServices()->workingDatabase()
->getGroupDictionary(pDict, AcDb::kForRead);
// Get an iterator for the ASDK_DICT dictionary.
//
AcDbDictionaryIterator* pDictIter = pDict->newIterator();
AcDbGroup *pGroup;
char* name;
for (; !pDictIter->done(); pDictIter->next()) {
// Get the current record, open it for read, and
// print its name.
//
pDictIter->getObject((AcDbObject*&)pGroup,
AcDb::kForRead);
pGroup->getName(name);
pGroup->close();
acutPrintf("\nintval is: %s", name);
}
delete pDictIter;
pDict->close();
}
//检测AutoCAD是否已经运行
CODE:
void Autocadtest()
{
// TODO: Add your control notification handler code here
IAcadApplication m_autocad;
IAcadDocuments m_acaddocs;
IAcadDocument m_acaddoc;
IAcadModelSpace m_acadmodel;
LPDISPATCH pDisp;
LPUNKNOWN pUnk;
CLSID clsid;
BeginWaitCursor();
::CLSIDFromProgID(L"AutoCAD.Application",&clsid);
if(::GetActiveObject(clsid,NULL,&pUnk)==S_OK)
{
VERIFY(pUnk->QueryInterface(IID_IDispatch,(void**) &pDisp)==S_OK);
m_autocad.AttachDispatch(pDisp);
pUnk->Release();
}
else
{
if(!m_autocad.CreateDispatch("AutoCAD.Application"))
{
AfxMessageBox("Autocad program not found\n");
exit(1);
}
}
m_autocad.SetVisible(true);
m_acaddocs.AttachDispatch(m_autocad.GetDocuments(),true);
m_acaddoc.AttachDispatch(m_acaddocs.Add(vtMissing),true);
m_acadmodel.AttachDispatch(m_acaddoc.GetModelSpace(),true);
m_acadmodel.AddCircle(pVal,100);
m_acadmodel.ReleaseDispatch();
m_acaddoc.ReleaseDispatch();
m_acaddocs.ReleaseDispatch();
m_autocad.ReleaseDispatch();
}
//计算多边形的形心坐标
BOOL GetPolyCentroid(AcDbPolyline * pPline, ads_point CenPt)
{
unsigned int i, iCount = 0;
AcDbVoidPtrArray curveSegments, regions;
AcGePoint3d LinePt0, LinePt1;
AcGePoint3d origin;
AcGeVector3d xAxis, yAxis;
double perimeter, area, prodInertia;
double momInertia[2], prinMoments[2], radiiGyration[2];
AcGePoint2d centroid;
AcGeVector2d prinAxes[2];
AcGePoint2d extentsLow, extentsHigh;
if (pPline->isClosed() != Adesk::kTrue) {
ads_printf("\n折线不封闭, 无法形成正确的区域。");
return FALSE;
}
curveSegments.append((AcDbCurve *) pPline);
if (AcDbRegion::createFromCurves(curveSegments, regions) != Acad::eOk){
ads_printf("\n创建临时区域对象失败!");
//清除Region, 应第9 贴的指点,即使createFromCurves错误,也应清除之;
iCount = regions.length();
for(i = 0; i < iCount; i++)
delete (AcDbRegion *)regions.at(i);
return FALSE;
}
AcDbRegion * pRegion;
if ((iCount = regions.length()) == 0){
ads_printf("\n创建临时区域对象为空!");
return FALSE;
}
if (iCount > 1){
// 多个 AcDbRegion , 无法确定应该返回哪一个,干脆返回NULL;
ads_printf("\n多个区域实体。");
for(i = 0; i < iCount; i++)
delete (AcDbRegion *)regions.at(i);
return FALSE;
}
pRegion = (AcDbRegion *) regions.at(0);
origin.set(0,0,0); //设置原点坐标
xAxis.set(1,0,0); //设置X Y轴,
yAxis.set(0,1,0);
if (pRegion->getAreaProp(
origin, xAxis, yAxis,
perimeter, area, centroid, momInertia, prodInertia, prinMoments, prinAxes, radiiGyration,
extentsLow, extentsHigh) != Acad::eOk ){
ads_printf("\n区域面积: %.3f, 周长:%.3f", area, perimeter);
ads_printf("\n获取区域对象属性失败!");
delete pRegion;
return FALSE;
}
XYZ_POINT(CenPt, centroid.x, centroid.y, 0); //得到形心坐标
ads_printf("\n区域面积: %.3f, 周长:%.3f", area, perimeter);
pRegion->close();
delete pRegion;
return TRUE;
}
AcDbObjectId CreateHatch(
AcDbObjectId dbOId,
char cLayer[],
char cPattern[] = "SOLID",
int nColor = 256,
double dAngle = 0.0,
double dScale = 1.0,
AcDbDatabase * pDbDatab = acdbHostApplicationServices()->workingDatabase())
{
AcCmColor CmC;
AcDbObjectId DbOId;
AcDbObjectIdArray DbOIdA(0, 2);
AcDbBlockTable * pDbBT;
AcDbBlockTableRecord * pDbBTR;
AcGeVector3d normal(0.0, 0.0, 1.0);
DbOIdA.append(dbOId);
AcDbHatch* pDbHat = new AcDbHatch();
pDbHat->setDatabaseDefaults();
pDbHat->setAssociative(Adesk::kTrue); // BUG: doesn't do squat! have to set the reactor yourself to get associativity!
pDbHat->appendLoop(AcDbHatch::kExternal, DbOIdA);
pDbHat->setPatternScale(dScale);
pDbHat->setPatternAngle(dAngle);
pDbHat->setPattern(AcDbHatch::kPreDefined, cPattern);
pDbHat->setNormal(normal);
pDbHat->evaluateHatch(); // crucial call or nothing gets displayed!
pDbDatab->getSymbolTable(pDbBT, AcDb::kForRead);
pDbBT->getAt(ACDB_MODEL_SPACE, pDbBTR, AcDb::kForWrite);
pDbBTR->appendAcDbEntity(DbOId, pDbHat);
pDbHat->setLayer(cLayer);
CmC.setColorIndex(nColor);
((AcDbEntity *)pDbHat)->setColor(CmC);
pDbBT->close();
pDbBTR->close();
pDbHat->close();
return DbOId;
}
objectARX 常用功能实现集合
一 在ARX中禁用AutoCAD的某个命令
以LINE命令为例,在程序中加入下面的一句即可禁用LINE命令:
acedCommand(RTSTR, "undefine", RTSTR, "line",RTNONE);
下面的语句则可恢复LINE命令的定义:
acedCommand(RTSTR, "redefine", RTSTR, "line",RTNONE);
二 在对话框中预览DWG文件
使用acdbDisplayPreviewFromDwg函数,具体的方法为:
char fileName[100];
strcpy(fileName, "C:\\test.dwg");
bool es;
HWND pWnd;
CFrameWnd *pFrame = (CFrameWnd*)GetDlgItem(IDC_PICTURE);
es = acdbDisplayPreviewFromDwg(fileName, pFrame->m_hWnd);
上面的代码将在一个Picture控件中显示指定的图形。
另外,需要包含“dbmain.h”头文件。
三 通过ARX更改AutoCAD窗口的标题名称
CMDIFrameWnd *pp;
pp=acedGetAcadFrame();
pp->SetWindowText ("yourName");
pp->UpdateWindow ();
四 获得当前数据库
在ARX编程中,经常需要使用当前数据库,例如需要获得当前图形中设置的文字样式、标注样式等。
要获得当前数据库,都可以直接使用下面的方法:
AcDbTextStyleTable *pTextStyleTAble;
AcDbObjectId textstyleId;
textstyleId=acdbHostApplicationServices()->workingDatabase()->textstyle();
如果用acadCurDwg来代替acdbHostApplicationServices()->workingDatabase(),也能得到同样的结果。