明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1773|回复: 1

求救::大家帮我看看红色部分出什么错了

[复制链接]
发表于 2007-5-1 16:04:00 | 显示全部楼层 |阅读模式

以下是我自己编的代码. 其中的红色部分的函数出了什么错 老出来 not open for write的错误

求高手教我改正


#include "StdAfx.h"
#include "StdArx.h"

#include "myline.h"

bool b_Replace=FALSE;
AcGePoint3d get_startpoint,get_endpoint;

void Del_Replaceline(Cmyline mylinex);
AcGePoint3d Judgement(Cmyline Cmyline1,Cmyline Cmyline2);

// This is command 'REPLINE'
void zzh_hfutrepline()
{
 // TODO: Implement the command
 AcDbDatabase *pCurrentDatabase;
 pCurrentDatabase = acdbHostApplicationServices()->workingDatabase();
 
    AcDbBlockTable *pBT;
 pCurrentDatabase->getSymbolTable(pBT, AcDb::kForRead);

 AcDbBlockTableRecord *pBTR;
 AcDbBlockTableIterator *pIterator;
 pBT->newIterator(pIterator);
 pBT->close();
 
 AcDbLine *pLine[20], *pLine1 = NULL;
 Cmyline myline0[20];  //自己定义的直线类,用于存储各个直线的端点
 int i=0, Count=0;
 
 for (pIterator->start();!pIterator->done();pIterator->step())
 {
  AcDbBlockTableRecord *pBTR;
  pIterator->getRecord(pBTR,AcDb::kForWrite,Adesk::kFalse);
  
  AcDbBlockTableRecordIterator *pIrterator;
  pBTR->newIterator(pIrterator);          //建立迭代器  
  pBTR->close();
  
  
        //完成将直线的端点存储在Cmyline中
  while(!pIrterator->done())
  {
   AcDbEntity *pEnt;
   pIrterator->getEntity(pEnt,AcDb::kForWrite); //打开实体,进行编辑
   if (strcmp(pEnt->isA()->name(),"AcDbLine") == 0)//若是线形,则继续
   {
    pLine=AcDbLine::cast(pEnt);
    pEnt->close();

    myline0.startpt = pLine->startPoint();//将直线的两端点加以保存
    myline0.endpt = pLine->endPoint();

    pLine->close();

    i++;
    Count++;
   }
   pIrterator->step();
  }
  delete pIrterator;
 
 }
 delete pIterator;
  
 
 for ( i=0; i<Count; i++)
 {
  for (int j=i+1; j<Count; j++)
  {
   Judgement(myline0, myline0[j]);

   if (b_Replace==TRUE)
   {
    Del_Replaceline(myline0);
    Del_Replaceline(myline0[j]);

    pBT->getAt(ACDB_MODEL_SPACE, pBTR,AcDb::kForWrite);
    pBT->close();
    
    AcDbObjectId acdbLineId;
    pLine1=new AcDbLine(get_startpoint,get_endpoint);//重画一条线代替重复的两条线
    
    pBTR->appendAcDbEntity(acdbLineId, pLine1);
    pBTR->close();
    
    pLine1->setColorIndex(1);
    pLine1->close();
   }
  }
 }

 
 
 for (i=0; i<Count; i++)
 {
  pLine->close();
 }
  
}


//实现比较两条线是否为重合
//判断最大距离的两点,并返回这两点。以便进行两线的合并。
//
AcGePoint3d Judgement(Cmyline myline1,Cmyline myline2)
{
 double fslope1, fslope2;//斜率
 double Sx1, Sy1, Ex1, Ey1;//比较时用到的第一条直线的两个端点坐标变量
 double Sx2, Sy2, Ex2, Ey2;//第二条直线端点坐标变量
 
 Sx1 = myline1.startpt.x;
 Sy1 = myline1.startpt.y;
 Ex1 = myline1.endpt.x;
 Ey1 = myline1.endpt.y;
 
 Sx2 = myline2.startpt.x;
 Sy2 = myline2.startpt.y;
 Ex2 = myline2.endpt.x;
 Ey2 = myline2.endpt.y;
 
 fslope1 = (Ey1 - Sy1) / (Ex1 - Sx1);
 fslope2 = (Ey2 - Sy2) / (Ex2 - Sx2);
 
 double Max_distance, distance1, distance2, distance3, distance4;
 
 distance1 = (Sx1 - Ex1) * (Sx1 - Ex1) + (Sy1 - Ey1) * (Sy1 - Ey1);//第一条直线两端点间的距离
 distance2 = (Sx2 - Ex2) * (Sx2 - Ex2) + (Sy2 - Ey2) * (Sy2 - Ey2);//第二条直线两端点间的距离
 distance3 = (Sx1 - Ex2) * (Sx1 - Ex2) + (Sy1 - Ey2) * (Sy1 - Ey2);//第一条直线起点于第二条直线终点间的距离
 distance4 = (Sx2 - Ex1) * (Sx2 - Ex1) + (Sy2 - Ey1) * (Sy2 - Ey1);//第一条直线终点与第二条直线起点间的距离
   
 //判断是否重合有三个条件
 //一、是否斜率相等
 //二、其中一条线的一端点是否在另一条直线的延长线上
 //三、其中一条线的一端点的横坐标是否在另一条直线的两个端点的横坐标之间
 if ((fslope1==fslope2)
  &&((Sy2==(Sx2-Sx1)*fslope1+Sy1)||(Ey2==(Ex2-Sx1)*fslope1+Sy1))
  &&((Sx1<=Sx2&&Sx2<=Ex1)||(Sx1<=Ex2&&Ex2<=Ex1)))
 {
  Max_distance = distance1;
  if (distance2>Max_distance)
   Max_distance = distance2;
  if (distance3>Max_distance)
   Max_distance = distance3;
  if (distance4>Max_distance)
   Max_distance = distance4;
  
  if (Max_distance==distance1)
  {
   get_startpoint.x = Sx1;
   get_startpoint.y = Sy1;
   get_endpoint.x = Ex1;
   get_endpoint.y = Ey1;
   
   b_Replace=TRUE;
   return get_endpoint,get_startpoint;
  }
  
  else if (Max_distance==distance2)
  {
   get_startpoint.x = Sx2;
   get_startpoint.y = Sy2;
   get_endpoint.x = Ex2;
   get_endpoint.y = Ey2;
   
   b_Replace=TRUE;
   return get_endpoint,get_startpoint;
  }
  
  else if (Max_distance==distance3)
  {
   get_startpoint.x = Sx1;
   get_startpoint.y = Sy1;
   get_endpoint.x = Ex2;
   get_endpoint.y = Ey2;
   
   b_Replace=TRUE;
   return get_endpoint,get_startpoint;
  }
  
  else if (Max_distance==distance4)
  {
   get_startpoint.x = Sx2;
   get_startpoint.y = Sy2;
   get_endpoint.x = Ex1;
   get_endpoint.y = Ey1;
   
   b_Replace=TRUE;
   return get_endpoint,get_startpoint;
  }
  
  else
  {
   ads_printf("\n error please restart the program !");
   
  }
 }
  
}

void Del_Replaceline(Cmyline mylinex)
{
 AcDbDatabase *pCurrentDatabase;
 pCurrentDatabase = acdbHostApplicationServices()->workingDatabase();
 
    AcDbBlockTable *pBT;
 pCurrentDatabase->getSymbolTable(pBT, AcDb::kForRead);
 

 AcDbBlockTableIterator *pIterator;
 pBT->newIterator(pIterator);
 pBT->close();
 for (pIterator->start();!pIterator->done();pIterator->step())
 {
  AcDbBlockTableRecord *pBTR;
  pIterator->getRecord(pBTR,AcDb::kForWrite,Adesk::kFalse);
  
  AcDbBlockTableRecordIterator *pIrterator;
  pBTR->newIterator(pIrterator);          //建立迭代器  
  pBTR->close();
  
  while(!pIrterator->done())
  {
   AcDbLine *pline_Del;
   AcDbEntity *pEnt;
   AcGePoint3d Del_Startpt,Del_Endpt;
   pIrterator->getEntity(pEnt,AcDb::kForWrite); //打开实体,进行编辑
   if (strcmp(pEnt->isA()->name(),"AcDbLine") == 0)//若是线形,则继续
   {
    pline_Del=AcDbLine::cast(pEnt);
    pEnt->close();

    Del_Startpt=pline_Del->startPoint();
    Del_Endpt=pline_Del->endPoint();
   
       if ((mylinex.startpt.x==Del_Startpt.x)&&(mylinex.startpt.y==Del_Startpt.y)
     &&(mylinex.endpt.x==Del_Endpt.x)&&(mylinex.endpt.y==Del_Endpt.y))
    {
     pBT->getAt(ACDB_MODEL_SPACE, pBTR,AcDb::kForWrite);
        pBT->close();

     AcDbObjectId acdbLineId;
     pline_Del->erase();
     
                    pBTR->appendAcDbEntity(acdbLineId, pline_Del);
          pBTR->close();

     pline_Del->close();
    }

    
   }
   pIrterator->step();
  }
  delete pIrterator; 
  
 }
 delete pIterator;
  
}

 楼主| 发表于 2007-5-2 12:55:00 | 显示全部楼层

问题已经解决 结贴

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

本版积分规则

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

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

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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