|
注意,也可使用 es = acdbLoadLineTypeFile(result.at(3), _T("zwcadiso.lin"), acdbCurDwg());来加载线型
pDb->loadLineTypeFile
对于线型中使用了文字的,也先加载文字样式
在使用LoadLineTypeFile从线型文件中(如acad.lin加载部分含有文字(形)的线型的莫名其妙的出错,经过一段时间的百般查找原因,终于明白,如 db.LoadLineTypeFile(“BATTING”, lineFile),要加BATTING线型,该线型描述如下:
*BATTING,Batting SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
A,.0001,-.1,[BAT,ltypeshp.shx,x=-.1,s=.1],-.2,[BAT,ltypeshp.shx,r=180,x=.1,s=.1],-.1
从中可以看到字母“S”需要的字体(形)为“BAT”,支持该字体(形)的文件为“ltypeshp.shx”。在加载“BATTING”线型失败,是由于db中字体表中缺少“BAT”字体(形)记录,因此,需要预先加载该字体。加载代码可如下:
//load font style
TextStyleTable tst = db.TextStyleTableId.GetObject(Teigha.DatabaseServices.OpenMode.ForWrite) as TextStyleTable;
TextStyleTableRecord Bat = new TextStyleTableRecord();
Bat.Name = "BAT";
string shapeFile = Application.StartupPath + "\\ltypeshp.shx";
tst.Add(Bat);
Bat.IsShapeFile = true;//要放在后面
Bat.FileName = shapeFile;//要放在后面
现在就可以成功加载"BATTING"线型了:
LinetypeTable ltt = db.LinetypeTableId.GetObject(Teigha.DatabaseServices.OpenMode.ForWrite) as LinetypeTable;
string lineFile = Application.StartupPath + "\\acad.lin";
db.LoadLineTypeFile("BATTING", lineFile);
创建图层记录:
void ZffCHAP4NewLayer()
{// 提示用户输入新建图层的名称char layerName[100];if (acedGetString(Adesk::kFalse, "\n输入新图层的名称:",layerName) != RTNORM) return;// 获得当前图形的层表AcDbLayerTable *pLayerTbl = NULL;
acdbHostApplicationServices()->workingDatabase()->getLayerTable(pLayerTbl, AcDb::kForWrite);// 是否已经包含指定的层表记录if (pLayerTbl->has(layerName))
{
pLayerTbl->close();return;
}// 创建新的层表记录AcDbLayerTableRecord *pLayerTblRcd = NULL;
pLayerTblRcd = new AcDbLayerTableRecord();
pLayerTblRcd->setName(layerName);// 将新建的层表记录添加到层表中AcDbObjectId layerTblRcdId;
pLayerTbl->add(layerTblRcdId, pLayerTblRcd);
acdbHostApplicationServices()->workingDatabase()->setClayer(layerTblRcdId);
pLayerTblRcd->close();
pLayerTbl->close();
}
修改指定图层的颜色
void ZffCHAP4LayerColor()
{// 提示用户输入要修改的图层名称char layerName[100];if (acedGetString(Adesk::kFalse, "\n输入图层的名称:",layerName) != RTNORM) return;// 获得当前图形的层表AcDbLayerTable *pLayerTbl = NULL;
acdbHostApplicationServices()->workingDatabase()->getLayerTable(pLayerTbl, AcDb::kForRead);// 判断是否包含指定名称的层表记录if (!pLayerTbl->has(layerName))
{
pLayerTbl->close();return;
}// 获得指定层表记录的指针AcDbLayerTableRecord *pLayerTblRcd = NULL;
pLayerTbl->getAt(layerName, pLayerTblRcd, AcDb::kForWrite);// 弹出“颜色”对话框AcCmColor oldColor = pLayerTblRcd->color();
int nCurColor = oldColor.colorIndex(); // 图层修改前的颜色int nNewColor = oldColor.colorIndex(); // 用户选择的颜色if (acedSetColorDialog(nNewColor, Adesk::kFalse, nCurColor))
{
AcCmColor color;
color.setColorIndex(nNewColor);
pLayerTblRcd->setColor(color);
}
pLayerTblRcd->close();
pLayerTbl->close();
}
删除指定的图层
void ZffCHAP4DelLayer()
{// 提示用户输入要修改的图层名称char layerName[100];if (acedGetString(Adesk::kFalse, "\n输入图层的名称:",layerName) != RTNORM)return;// 获得当前图形的层表AcDbLayerTable *pLayerTbl = NULL;
acdbHostApplicationServices()->workingDatabase()->getLayerTable(pLayerTbl, AcDb::kForRead);// 判断是否包含指定名称的层表记录if (!pLayerTbl->has(layerName))
{
pLayerTbl->close();return;
}// 获得指定层表记录的指针AcDbLayerTableRecord *pLayerTblRcd = NULL;
pLayerTbl->getAt(layerName, pLayerTblRcd, AcDb::kForWrite);
pLayerTblRcd->erase(); // 为其设置“删除”标记pLayerTblRcd->close();
pLayerTbl->close();
}
所有图层及其特性导出到文本文件
void ZffCHAP4ExportLayer()
{// 创建所要导出的文本文件CStdioFile f;
CFileException e;
char *pFileName = "C:\\layers.txt";if (!f.Open(pFileName, CFile::modeCreate | CFile::modeWrite, &e))
{
acutPrintf("\n创建导出文件失败!");return;
}// 获得层表指针AcDbLayerTable *pLayerTbl = NULL;
acdbHostApplicationServices()->workingDatabase()->getLayerTable(pLayerTbl, AcDb::kForRead);// 使用遍历器访问每一条层表记录AcDbLayerTableIterator *pItr = NULL;
AcDbLayerTableRecord *pLayerTblRcd = NULL;
pLayerTbl->newIterator(pItr);for (pItr->start(); !pItr->done(); pItr->step())
{
pItr->getRecord(pLayerTblRcd, AcDb::kForRead);// 输出图层的信息CString strLayerInfo; // 图层名称char *layerName = NULL;
pLayerTblRcd->getName(layerName);
strLayerInfo = layerName;
free(layerName);
strLayerInfo += ","; // 分隔符CString strColor; // 图层颜色AcCmColor color = pLayerTblRcd->color();
strColor.Format("%d", color.colorIndex());
strLayerInfo += strColor;
strLayerInfo += ",";
CString strLinetype; // 图层线型AcDbLinetypeTableRecord *pLinetypeTblRcd = NULL;
acdbOpenObject(pLinetypeTblRcd,pLayerTblRcd->linetypeObjectId(),AcDb::kForRead);
char *linetypeName = NULL;
pLinetypeTblRcd->getName(linetypeName);
pLinetypeTblRcd->close();
strLinetype = linetypeName;
free(linetypeName);
strLayerInfo += strLinetype;
strLayerInfo += ",";
CString strLineWeight; // 图层的线宽AcDb::LineWeight lineWeight = pLayerTblRcd->lineWeight();
strLineWeight.Format("%d", lineWeight);
strLayerInfo += strLineWeight;// 将图层特性写入到文件中f.WriteString(strLayerInfo);
f.WriteString("\n");
pLayerTblRcd->close();
}
delete pItr;
pLayerTbl->close();
}
根据文本文件导入所有图层及其特性
//读取一行文本之后,需要根据分隔符( “,” )来解析出图层的名称、颜色、线型和线宽BOOL GetFieldText(CString strLineText, CStringArray &fields)
{
if (strLineText.Find(",", 0) == -1) // 如果找不到英文逗号,函数退出
{return FALSE;
}
int nLeftPos = 0, nRightPos = 0; // 查找分隔符的起始位置while ((nRightPos = strLineText.Find(",", nRightPos)) != -1)
{
fields.Add(strLineText.Mid(nLeftPos, nRightPos - nLeftPos));
nLeftPos = nRightPos + 1;
nRightPos++;
}// 最后一个列的数据fields.Add(strLineText.Mid(nLeftPos));return TRUE;
}
void ZffCHAP4ImportLayer()
{// 打开所要导入的文本文件CStdioFile f;
CFileException e;
char *pFileName = "C:\\layers.txt";if (!f.Open(pFileName, CFile::modeRead, &e))
{
acutPrintf("\n打开导入文件失败!");return;
}// 获得层表指针AcDbLayerTable *pLayerTbl = NULL;
AcDbLayerTableRecord *pLayerTblRcd = NULL;
acdbHostApplicationServices()->workingDatabase()->getLayerTable(pLayerTbl, AcDb::kForWrite);// 读取文件中的每一行数据CString strLineText; // 一行文字while (f.ReadString(strLineText))
{// 跳过空行if (strLineText.IsEmpty()) continue;// 解析出图层名称、颜色、线型和线宽CStringArray layerInfos;if (!GetFieldText(strLineText, layerInfos)) continue;// 创建新的层表记录,或者打开存在的块表记录AcDbLayerTableRecord *pLayerTblRcd = NULL;
AcDbObjectId layerTblRcdId;if (pLayerTbl->has(layerInfos.GetAt(0)))
{
pLayerTbl->getAt(layerInfos.GetAt(0), layerTblRcdId);
}else{
pLayerTblRcd = new AcDbLayerTableRecord();
pLayerTblRcd->setName(layerInfos.GetAt(0));
pLayerTbl->add(layerTblRcdId, pLayerTblRcd);
pLayerTblRcd->close();
}
acdbOpenObject(pLayerTblRcd, layerTblRcdId,AcDb::kForWrite);// 设置层表记录的颜色AcCmColor color;
Adesk::UInt16 colorIndex = atoi(layerInfos.GetAt(1));
color.setColorIndex(colorIndex);
pLayerTblRcd->setColor(color);// 设置线型AcDbLinetypeTable *pLinetypeTbl = NULL;
AcDbObjectId linetypeId;
acdbHostApplicationServices()->workingDatabase()->getLinetypeTable(pLinetypeTbl,AcDb::kForRead);if (pLinetypeTbl->has(layerInfos.GetAt(2)))
{
pLinetypeTbl->getAt(layerInfos.GetAt(2), linetypeId);
}else{
pLinetypeTbl->getAt("Continous", linetypeId);
}
pLayerTblRcd->setLinetypeObjectId(linetypeId);
pLinetypeTbl->close();// 设置线宽AcDb::LineWeight lineWeight =(AcDb::LineWeight)atol(layerInfos.GetAt(3));
pLayerTblRcd->setLineWeight(lineWeight);
pLayerTblRcd->close();
}
pLayerTbl->close();
} |
|