[求助]还是创建图层的问题
<p>我在编写创建图层时遇到了一个匪夷所思的问题,和大家分享一下,希望能有高手帮忙解决! 另外 我初学ARX 希望大家多指点!</p><p>这是我编写的创建图层的函数,放在自己添加的 CCreateEnt 类中 </p><p>AcDbObjectId CCreateEnt::CreatLayer (char layername,int nNewColor,<br/> int Weight,char linetype)<br/>{<br/> // 获得当前图形的层表<br/> AcDbLayerTable *pLayerTbl;<br/> acdbHostApplicationServices()->workingDatabase()<br/> ->getLayerTable(pLayerTbl, AcDb::kForWrite);<br/> <br/> // 创建新的层表记录<br/> AcDbObjectId layerTblRcdId;<br/> AcDbLayerTableRecord *pLayerTblRcd;<br/> pLayerTblRcd = new AcDbLayerTableRecord();</p><p> pLayerTblRcd->setName(layername);<br/> AcCmColor color;<br/> color.setColorIndex(nNewColor);<br/> pLayerTblRcd->setColor(color);<br/> AcDb::LineWeight lineWeight =(AcDb::LineWeight)Weight;<br/> pLayerTblRcd->setLineWeight(lineWeight);</p><p> AcDbLinetypeTable *pLinetypeTbl;<br/> acdbHostApplicationServices()->workingDatabase()<br/> ->getLinetypeTable(pLinetypeTbl, AcDb::kForWrite);<br/> AcDbObjectId linetypeId;<br/> pLinetypeTbl->getAt(linetype, linetypeId);<br/> pLayerTblRcd->setLinetypeObjectId(linetypeId);<br/> pLinetypeTbl->close();<br/> </p><p> // 将新建的层表记录添加到层表中<br/> </p><p>pLayerTbl->add(layerTblRcdId, pLayerTblRcd);</p><p> pLayerTbl->close();<br/> pLayerTblRcd->close();</p><p> return layerTblRcdId;<br/>}</p><p>以下是我在acrxEnryPoint 中调用的 函数:</p><p>static void ShipMyrun(void)<br/> { <br/> // Add your code for command ShipMy.run here<br/> char name1[]="中心线";<br/> char name2[]="虚线";<br/> char name3[]="粗实线";<br/> char linetype1[]="CENTER";<br/> char linetype2[]="DASHED";<br/> char linetype3[]="Continuous";</p><p> AcDbLayerTable *pLayerTbl;<br/> AcDbObjectId layerTblRcdId;<br/> acdbHostApplicationServices()->workingDatabase()<br/> ->getLayerTable(pLayerTbl, AcDb::kForRead);<br/> if ((pLayerTbl->getAt(name1,layerTblRcdId))!=Acad::eOk) <br/> {<br/> CCreateEnt::CreatLayer(name1,1,0,linetype1); <br/> }else{<br/> acutPrintf("\n图层以创建!");}</p><p> if ((pLayerTbl->getAt(name2,layerTblRcdId))!=Acad::eOk)<br/> {<br/> CCreateEnt::CreatLayer(name2,50,0,linetype2);<br/> }else{<br/> acutPrintf("\n图层以创建!");}</p><p> if ((pLayerTbl->getAt(name3,layerTblRcdId))!=Acad::eOk)<br/> {<br/> CCreateEnt::CreatLayer(name3,100,30,linetype3);<br/> }else{<br/> acutPrintf("\n图层以创建!");}</p><p> acutPrintf("\n创建图层任务已完成!");<br/> pLayerTbl->close();<br/> }</p><p>编译没有问题,但在CAD中无法运行,导致CAD致命错误。而且我试过了:</p><p>static void ShipMyrun(void)<br/> { <br/> // Add your code for command ShipMy.run here<br/> char name1[]="中心线";<br/> char name2[]="虚线";<br/> char name3[]="粗实线";<br/> char linetype1[]="CENTER";<br/> char linetype2[]="DASHED";<br/> char linetype3[]="Continuous";</p><p> //AcDbLayerTable *pLayerTbl;<br/> //AcDbObjectId layerTblRcdId;<br/> //acdbHostApplicationServices()->workingDatabase()<br/> //->getLayerTable(pLayerTbl, AcDb::kForRead);<br/> //if ((pLayerTbl->getAt(name1,layerTblRcdId))!=Acad::eOk) <br/> //{<br/> CCreateEnt::CreatLayer(name1,1,0,linetype1); <br/> //}else{<br/> //acutPrintf("\n图层以创建!");}</p><p> //if ((pLayerTbl->getAt(name2,layerTblRcdId))!=Acad::eOk)<br/> //{<br/> CCreateEnt::CreatLayer(name2,50,0,linetype2);<br/> //}else{<br/> //acutPrintf("\n图层以创建!");}</p><p> //if ((pLayerTbl->getAt(name3,layerTblRcdId))!=Acad::eOk)<br/> //{<br/> CCreateEnt::CreatLayer(name3,100,30,linetype3);<br/> //}else{<br/> //acutPrintf("\n图层以创建!");}</p><p> //acutPrintf("\n创建图层任务已完成!");<br/> //pLayerTbl->close();<br/> }</p><p>注释掉 判断语句后 却可以正确执行,另外我也试过 注释掉 创建图层的函数 加一条输出字符 函数,也运行成功了, 这使我我感到匪夷所思,请哪位高手指点一下! </p><p>static void ShipMyrun(void)<br/> { <br/> // Add your code for command ShipMy.run here<br/> char name1[]="中心线";<br/> char name2[]="虚线";<br/> char name3[]="粗实线";<br/> char linetype1[]="CENTER";<br/> char linetype2[]="DASHED";<br/> char linetype3[]="Continuous";</p><p> AcDbLayerTable *pLayerTbl;<br/> AcDbObjectId layerTblRcdId;<br/> acdbHostApplicationServices()->workingDatabase()<br/> ->getLayerTable(pLayerTbl, AcDb::kForRead);<br/> if ((pLayerTbl->getAt(name1,layerTblRcdId))!=Acad::eOk) <br/> {<br/> //CCreateEnt::CreatLayer(name1,1,0,linetype1);<br/> acutPrintf("\n图层以创建!");</p><p> }else{<br/> acutPrintf("\n图层以创建!");}</p><p> if ((pLayerTbl->getAt(name2,layerTblRcdId))!=Acad::eOk)<br/> {<br/> //CCreateEnt::CreatLayer(name2,50,0,linetype2);<br/> acutPrintf("\n图层以创建!");<br/> }else{<br/> acutPrintf("\n图层以创建!");}</p><p> if ((pLayerTbl->getAt(name3,layerTblRcdId))!=Acad::eOk)<br/> {<br/> //CCreateEnt::CreatLayer(name3,100,30,linetype3);<br/> acutPrintf("\n图层以创建!");<br/> }else{<br/> acutPrintf("\n图层以创建!");}</p><p> acutPrintf("\n创建图层任务已完成!");<br/> pLayerTbl->close();<br/> }</p> <p>你在CreatLayer函数中</p><p>acdbHostApplicationServices()->workingDatabase()<br/> ->getLayerTable(pLayerTbl, AcDb::kForWrite);<br/></p><p>而在你外面调用</p><p>acdbHostApplicationServices()->workingDatabase()<br/> ->getLayerTable(pLayerTbl, AcDb::kForRead);</p><p>当然要崩溃!</p><p>你创建之间的判断可以放到函数里面去判断</p> <p>嗯 我按你说的把 判断 放在 CreateLayer 里面了</p><p>AcDbObjectId CCreateEnt::CreatLayer (char layername,int nNewColor,<br/> int Weight,char linetype)<br/>{</p><p> // 获得当前图形的层表<br/> AcDbLayerTable *pLayerTbl;<br/> AcDbObjectId layerTblRcdId;<br/> acdbHostApplicationServices()->workingDatabase()<br/> ->getLayerTable(pLayerTbl, AcDb::kForWrite);<br/> if ((pLayerTbl->getAt(layername,layerTblRcdId))!=Acad::eOk) <br/> {<br/> <br/> // 创建新的层表记录<br/> AcDbObjectId layerTblRcdId;<br/> AcDbLayerTableRecord *pLayerTblRcd;<br/> pLayerTblRcd = new AcDbLayerTableRecord();</p><p> pLayerTblRcd->setName(layername);<br/> AcCmColor color;<br/> color.setColorIndex(nNewColor);<br/> pLayerTblRcd->setColor(color);<br/> AcDb::LineWeight lineWeight =(AcDb::LineWeight)Weight;<br/> pLayerTblRcd->setLineWeight(lineWeight);</p><p> AcDbLinetypeTable *pLinetypeTbl;<br/> acdbHostApplicationServices()->workingDatabase()<br/> ->getLinetypeTable(pLinetypeTbl, AcDb::kForWrite);<br/> AcDbObjectId linetypeId;<br/> pLinetypeTbl->getAt(linetype, linetypeId);<br/> pLayerTblRcd->setLinetypeObjectId(linetypeId);<br/> pLinetypeTbl->close();<br/> </p><p> // 将新建的层表记录添加到层表中<br/> <br/> pLayerTbl->add(layerTblRcdId, pLayerTblRcd);</p><p> pLayerTbl->close();<br/> pLayerTblRcd->close();<br/> acutPrintf("\n图层创建成功!");</p><p> return layerTblRcdId;<br/> }else{<br/> acutPrintf("\n图层以创建!");}<br/>}</p><p>但编译时 有这么个 警告 warning C4715: “CCreateEnt::CreatLayer” : 不是所有的控件路径都返回值。</p><p>会对程序造成影响么?</p> <p>嗯 这次 彻底解决了 谢谢!</p><p>AcDbObjectId CCreateEnt::CreatLayer (char layername,int nNewColor,<br/> int Weight,char linetype)<br/>{</p><p> // 获得当前图形的层表<br/> AcDbLayerTable *pLayerTbl;<br/> AcDbObjectId layerTblRcdId;<br/> acdbHostApplicationServices()->workingDatabase()<br/> ->getLayerTable(pLayerTbl, AcDb::kForWrite);<br/> if ((pLayerTbl->getAt(layername,layerTblRcdId))!=Acad::eOk) <br/> {<br/> <br/> // 创建新的层表记录<br/> AcDbObjectId layerTblRcdId;<br/> AcDbLayerTableRecord *pLayerTblRcd;<br/> pLayerTblRcd = new AcDbLayerTableRecord();</p><p> pLayerTblRcd->setName(layername);<br/> AcCmColor color;<br/> color.setColorIndex(nNewColor);<br/> pLayerTblRcd->setColor(color);<br/> AcDb::LineWeight lineWeight =(AcDb::LineWeight)Weight;<br/> pLayerTblRcd->setLineWeight(lineWeight);</p><p> AcDbLinetypeTable *pLinetypeTbl;<br/> acdbHostApplicationServices()->workingDatabase()<br/> ->getLinetypeTable(pLinetypeTbl, AcDb::kForWrite);<br/> AcDbObjectId linetypeId;<br/> pLinetypeTbl->getAt(linetype, linetypeId);<br/> pLayerTblRcd->setLinetypeObjectId(linetypeId);<br/> pLinetypeTbl->close();<br/> </p><p> // 将新建的层表记录添加到层表中<br/> <br/> pLayerTbl->add(layerTblRcdId, pLayerTblRcd);</p><p> pLayerTbl->close();<br/> pLayerTblRcd->close();<br/> acutPrintf("\n [%s层] 创建成功!",layername);</p><p> return layerTblRcdId;<br/> }else{<br/> AcDbObjectId layerTblRcdId;<br/> pLayerTbl->getAt(layername,layerTblRcdId);<br/> return layerTblRcdId;<br/> acutPrintf("\n [%s层] 以创建!",layername);<br/> }<br/>}</p><p>生成:1 已成功, 0 已失败, 0 已跳过</p>
页:
[1]