明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2647|回复: 3

[求助]还是创建图层的问题

[复制链接]
发表于 2009-2-20 20:43:00 | 显示全部楼层 |阅读模式

我在编写创建图层时遇到了一个匪夷所思的问题,和大家分享一下,希望能有高手帮忙解决! 另外 我初学ARX  希望大家多指点!

这是我编写的创建图层的函数,放在自己添加的 CCreateEnt  类中

AcDbObjectId CCreateEnt::CreatLayer (char layername[100],int nNewColor,
          int Weight,char linetype[50])
{
 // 获得当前图形的层表
 AcDbLayerTable *pLayerTbl;
 acdbHostApplicationServices()->workingDatabase()
  ->getLayerTable(pLayerTbl, AcDb::kForWrite);
 
 // 创建新的层表记录
 AcDbObjectId layerTblRcdId;
 AcDbLayerTableRecord *pLayerTblRcd;
 pLayerTblRcd = new AcDbLayerTableRecord();

 pLayerTblRcd->setName(layername);
 AcCmColor color;
 color.setColorIndex(nNewColor);
 pLayerTblRcd->setColor(color);
 AcDb:ineWeight lineWeight =(AcDb:ineWeight)Weight;
 pLayerTblRcd->setLineWeight(lineWeight);

 AcDbLinetypeTable *pLinetypeTbl;
 acdbHostApplicationServices()->workingDatabase()
  ->getLinetypeTable(pLinetypeTbl, AcDb::kForWrite);
 AcDbObjectId linetypeId;
 pLinetypeTbl->getAt(linetype, linetypeId);
 pLayerTblRcd->setLinetypeObjectId(linetypeId);
 pLinetypeTbl->close();
 

 // 将新建的层表记录添加到层表中
 

pLayerTbl->add(layerTblRcdId, pLayerTblRcd);

 pLayerTbl->close();
 pLayerTblRcd->close();

 return layerTblRcdId;
}

以下是我在acrxEnryPoint  中调用的 函数:

static void ShipMyrun(void)
 { 
  // Add your code for command ShipMy.run here
  char name1[]="中心线";
  char name2[]="虚线";
  char name3[]="粗实线";
  char linetype1[]="CENTER";
  char linetype2[]="DASHED";
  char linetype3[]="Continuous";

  AcDbLayerTable *pLayerTbl;
  AcDbObjectId layerTblRcdId;
  acdbHostApplicationServices()->workingDatabase()
  ->getLayerTable(pLayerTbl, AcDb::kForRead);
  if  ((pLayerTbl->getAt(name1,layerTblRcdId))!=Acad::eOk) 
  {
  CCreateEnt::CreatLayer(name1,1,0,linetype1); 
  }else{
  acutPrintf("\n图层以创建!");}

  if ((pLayerTbl->getAt(name2,layerTblRcdId))!=Acad::eOk)
  {
  CCreateEnt::CreatLayer(name2,50,0,linetype2);
  }else{
  acutPrintf("\n图层以创建!");}

  if ((pLayerTbl->getAt(name3,layerTblRcdId))!=Acad::eOk)
  {
  CCreateEnt::CreatLayer(name3,100,30,linetype3);
  }else{
  acutPrintf("\n图层以创建!");}

  acutPrintf("\n创建图层任务已完成!");
  pLayerTbl->close();
 }

编译没有问题,但在CAD中无法运行,导致CAD致命错误。而且我试过了:

static void ShipMyrun(void)
 { 
  // Add your code for command ShipMy.run here
  char name1[]="中心线";
  char name2[]="虚线";
  char name3[]="粗实线";
  char linetype1[]="CENTER";
  char linetype2[]="DASHED";
  char linetype3[]="Continuous";

  //AcDbLayerTable *pLayerTbl;
  //AcDbObjectId layerTblRcdId;
  //acdbHostApplicationServices()->workingDatabase()
  //->getLayerTable(pLayerTbl, AcDb::kForRead);
  //if  ((pLayerTbl->getAt(name1,layerTblRcdId))!=Acad::eOk) 
  //{
  CCreateEnt::CreatLayer(name1,1,0,linetype1); 
  //}else{
  //acutPrintf("\n图层以创建!");}

  //if ((pLayerTbl->getAt(name2,layerTblRcdId))!=Acad::eOk)
  //{
  CCreateEnt::CreatLayer(name2,50,0,linetype2);
  //}else{
  //acutPrintf("\n图层以创建!");}

  //if ((pLayerTbl->getAt(name3,layerTblRcdId))!=Acad::eOk)
  //{
  CCreateEnt::CreatLayer(name3,100,30,linetype3);
  //}else{
  //acutPrintf("\n图层以创建!");}

  //acutPrintf("\n创建图层任务已完成!");
  //pLayerTbl->close();
 }

注释掉 判断语句后 却可以正确执行,另外我也试过 注释掉 创建图层的函数 加一条输出字符 函数,也运行成功了, 这使我我感到匪夷所思,请哪位高手指点一下! 

static void ShipMyrun(void)
 { 
  // Add your code for command ShipMy.run here
  char name1[]="中心线";
  char name2[]="虚线";
  char name3[]="粗实线";
  char linetype1[]="CENTER";
  char linetype2[]="DASHED";
  char linetype3[]="Continuous";

  AcDbLayerTable *pLayerTbl;
  AcDbObjectId layerTblRcdId;
  acdbHostApplicationServices()->workingDatabase()
  ->getLayerTable(pLayerTbl, AcDb::kForRead);
  if  ((pLayerTbl->getAt(name1,layerTblRcdId))!=Acad::eOk) 
  {
  //CCreateEnt::CreatLayer(name1,1,0,linetype1);
   acutPrintf("\n图层以创建!");

  }else{
  acutPrintf("\n图层以创建!");}

  if ((pLayerTbl->getAt(name2,layerTblRcdId))!=Acad::eOk)
  {
  //CCreateEnt::CreatLayer(name2,50,0,linetype2);
   acutPrintf("\n图层以创建!");
  }else{
  acutPrintf("\n图层以创建!");}

  if ((pLayerTbl->getAt(name3,layerTblRcdId))!=Acad::eOk)
  {
  //CCreateEnt::CreatLayer(name3,100,30,linetype3);
   acutPrintf("\n图层以创建!");
  }else{
  acutPrintf("\n图层以创建!");}

  acutPrintf("\n创建图层任务已完成!");
  pLayerTbl->close();
 }

发表于 2009-2-20 21:31:00 | 显示全部楼层

你在CreatLayer函数中

acdbHostApplicationServices()->workingDatabase()
  ->getLayerTable(pLayerTbl, AcDb::kForWrite);

而在你外面调用

acdbHostApplicationServices()->workingDatabase()
  ->getLayerTable(pLayerTbl, AcDb::kForRead);

当然要崩溃!

你创建之间的判断可以放到函数里面去判断

 楼主| 发表于 2009-2-20 22:05:00 | 显示全部楼层

嗯  我按你说的把 判断 放在 CreateLayer 里面了

AcDbObjectId CCreateEnt::CreatLayer (char layername[100],int nNewColor,
          int Weight,char linetype[50])
{

 // 获得当前图形的层表
 AcDbLayerTable *pLayerTbl;
  AcDbObjectId layerTblRcdId;
 acdbHostApplicationServices()->workingDatabase()
  ->getLayerTable(pLayerTbl, AcDb::kForWrite);
 if  ((pLayerTbl->getAt(layername,layerTblRcdId))!=Acad::eOk) 
 {
 
 // 创建新的层表记录
  AcDbObjectId layerTblRcdId;
  AcDbLayerTableRecord *pLayerTblRcd;
  pLayerTblRcd = new AcDbLayerTableRecord();

  pLayerTblRcd->setName(layername);
  AcCmColor color;
  color.setColorIndex(nNewColor);
  pLayerTblRcd->setColor(color);
  AcDb:ineWeight lineWeight =(AcDb:ineWeight)Weight;
  pLayerTblRcd->setLineWeight(lineWeight);

  AcDbLinetypeTable *pLinetypeTbl;
  acdbHostApplicationServices()->workingDatabase()
  ->getLinetypeTable(pLinetypeTbl, AcDb::kForWrite);
  AcDbObjectId linetypeId;
  pLinetypeTbl->getAt(linetype, linetypeId);
  pLayerTblRcd->setLinetypeObjectId(linetypeId);
  pLinetypeTbl->close();
 

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

  pLayerTbl->close();
  pLayerTblRcd->close();
  acutPrintf("\n图层创建成功!");

  return layerTblRcdId;
  }else{
  acutPrintf("\n图层以创建!");}
}

但编译时 有这么个 警告  warning C4715: “CCreateEnt::CreatLayer” : 不是所有的控件路径都返回值。

会对程序造成影响么?

 楼主| 发表于 2009-2-20 22:33:00 | 显示全部楼层

嗯  这次 彻底解决了 谢谢!

AcDbObjectId CCreateEnt::CreatLayer (char layername[100],int nNewColor,
          int Weight,char linetype[50])
{

 // 获得当前图形的层表
 AcDbLayerTable *pLayerTbl;
  AcDbObjectId layerTblRcdId;
 acdbHostApplicationServices()->workingDatabase()
  ->getLayerTable(pLayerTbl, AcDb::kForWrite);
 if  ((pLayerTbl->getAt(layername,layerTblRcdId))!=Acad::eOk) 
 {
 
 // 创建新的层表记录
  AcDbObjectId layerTblRcdId;
  AcDbLayerTableRecord *pLayerTblRcd;
  pLayerTblRcd = new AcDbLayerTableRecord();

  pLayerTblRcd->setName(layername);
  AcCmColor color;
  color.setColorIndex(nNewColor);
  pLayerTblRcd->setColor(color);
  AcDb:ineWeight lineWeight =(AcDb:ineWeight)Weight;
  pLayerTblRcd->setLineWeight(lineWeight);

  AcDbLinetypeTable *pLinetypeTbl;
  acdbHostApplicationServices()->workingDatabase()
  ->getLinetypeTable(pLinetypeTbl, AcDb::kForWrite);
  AcDbObjectId linetypeId;
  pLinetypeTbl->getAt(linetype, linetypeId);
  pLayerTblRcd->setLinetypeObjectId(linetypeId);
  pLinetypeTbl->close();
 

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

  pLayerTbl->close();
  pLayerTblRcd->close();
  acutPrintf("\n [%s层] 创建成功!",layername);

  return layerTblRcdId;
  }else{
   AcDbObjectId layerTblRcdId;
   pLayerTbl->getAt(layername,layerTblRcdId);
   return  layerTblRcdId;
   acutPrintf("\n [%s层] 以创建!",layername);
  }
}

生成:1 已成功, 0 已失败, 0 已跳过

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 14:20 , Processed in 0.177627 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表