|
ObjectARX-给cad实体贴图
做cad二次开发的时候,有时会生成三维实体,例如生成3d的钢筋,但是使用3d生成的钢筋比较难看,可以使用下面代码对实体进行贴图,比如找一张铁锈的图片放在钢筋上,就会显示的比较生动
AcDbObjectId CBaseDwg::CreateMaterial(const CString & materialName, COLORREF clr, const CString & pictureFilePath, AcDbDatabase * pDb)
{
if (NULL == pDb)
pDb = acdbCurDwg();
AcDbObjectId idMater = AcDbObjectId::kNull;
AcDbDictionary* pMatDict = NULL;
if (Acad::eOk != pDb->getMaterialDictionary(pMatDict, AcDb::kForWrite))
return idMater;
if (pMatDict->has(materialName))
{
pMatDict->getAt(materialName, idMater);
}
else
{
AcDbMaterial* pmatObj = new AcDbMaterial;
pMatDict->setAt(materialName, pmatObj, idMater);
//材质设置
AcGiMaterialColor clrMat;
clrMat.setMethod(AcGiMaterialColor::kOverride);
clrMat.setFactor(1.0);
clrMat.setColor(AcCmEntityColor(GetRValue(clr), GetGValue(clr), GetBValue(clr)));
//添加图片
AcGiImageFileTexture diffuseMapFile;
diffuseMapFile.setSourceFileName(pictureFilePath);
//diffuse map
AcGiMaterialMap diffuseMap;
diffuseMap.setTexture(&diffuseMapFile);
//控制贴图比例
AcGeMatrix3d transform;
transform.setToIdentity();
transform.setToScaling(0.1);
AcGiMapper mapper;
mapper = diffuseMap.mapper();
mapper.setTransform(transform);
diffuseMap.setMapper(mapper);
pmatObj->setDiffuse(clrMat, diffuseMap);
pmatObj->setMode(AcGiMaterialTraits::kRealistic);
double value = 1.0;
AcGiMaterialMap opacityMap;
pmatObj->opacity(value, opacityMap);
pmatObj->setOpacity(20, opacityMap);//?
pmatObj->close();
}
pMatDict->close();
return idMater;
}
void CBaseDwg::SetMaterialToEntity(const AcDbObjectId &entId, const AcDbObjectId & materialId)
{
AcDbEntityPointer pointer;
if (Acad::eOk != pointer.open(entId, AcDb::kForWrite))
return;
pointer->setMaterial(materialId, Adesk::kFalse);
} |
|