ObjectArx layer operations Summary - New, layer color, delete

ObjectArx layer operations summary

Refer to the online collection of information, through the field, ObjectArx2014 and 2018 can also be used, other versions should be no problem. The text of the many comments in great detail, the desire to help newcomers.

1. Create a new layer

CLayerOperator NewLayer defined function, the operation is completed for the new layer, the complete code NewLayer follows:

	// 新建一个图层
	void CLayerOperator::NewLayer(ACHAR* layerName)
	{
	    //获得当前图形的层表
	    AcDbLayerTable *pLayerTbl;
	    acdbHostApplicationServices()
	        ->workingDatabase()
	        ->getLayerTable(pLayerTbl,AcDb::kForWrite);

    //是否已经包含指定的层表记录
    if(pLayerTbl->has(layerName)){
        pLayerTbl->close();
        acedPrompt(_T("对应名称的图层已经存在!"));
        return;
    }

    //创建层表记录
    AcDbLayerTableRecord *pLayerTblRcd;
    pLayerTblRcd=new AcDbLayerTableRecord();
    pLayerTblRcd->setName(layerName);	//这里的layerName需要用(_T("?"))这种格式来命名

    //将新建的层表记录添加到层表中
    AcDbObjectId layerTblRcdId;
    pLayerTbl->add(layerTblRcdId,pLayerTblRcd);

    //设置图形的当前图层
    acdbHostApplicationServices()
        ->workingDatabase()
        ->setClayer(layerTblRcdId);
	//关闭层表
    pLayerTblRcd->close();
    pLayerTbl->close();
	}

NewLayer specific implementation comments have been very detailed, first obtain the current layer graphics table, and then specify whether the layer name already exists judge layer table, and if so, direct return, or create a table recording layer and the new layer table records layer is added to the table, and finally setClayer class will function AcDbDatabase new layer to the current layer is provided by.

2. Graphic dialog box to set the color specified layer

// 使用图形对话框设置指定图层的颜色
void CLayerOperator::SetLayerColor(ACHAR* layerName)
{
    //获得当前图形的层表
    AcDbLayerTable *pLayerTbl;
    acdbHostApplicationServices()
        ->workingDatabase()
        ->getLayerTable(pLayerTbl,AcDb::kForRead);
    //判断是否包含指定名称的层表记录
    if(!pLayerTbl->has(layerName)){
        pLayerTbl->close();
        return;
    }
    //获得指定层表记录的指针
    AcDbLayerTableRecord *pLayerTblRcd;
    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();
}

3. To delete a layer

// 删除一个图层
void CLayerOperator::DelLayer(ACHAR* layerName)
{
    //获得当前图形的层表
    AcDbLayerTable *pLayerTbl;
    acdbHostApplicationServices()
        ->workingDatabase()
        ->getLayerTable(pLayerTbl,AcDb::kForRead);
    //判断是否包含指定名称的层表记录
    if(!pLayerTbl->has(layerName)){
        pLayerTbl->close();
        return;
    }
    //获得指定层表记录的指针
    AcDbLayerTableRecord *pLayerTblRcd;
    pLayerTbl->getAt(layerName,pLayerTblRcd,AcDb::kForWrite);
    //为其设置“删除”标记
    pLayerTblRcd->erase();
    pLayerTblRcd->close();		//关闭层表
    pLayerTbl->close();
}

Thanks to the following article:
http://vcsos.com/article/pageSource/120413/20120413103600_1.shtml

Original articles published 0 · won praise 0 · Views 14

Guess you like

Origin blog.csdn.net/qq_38412748/article/details/104107823