明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2564|回复: 2

对象的关联

[复制链接]
发表于 2005-9-9 11:19:00 | 显示全部楼层 |阅读模式

exam10b.cpp

#include "string.h"
#include "stdlib.h"
#include "aced.h"
#include "dbents.h"
#include "dbsymtb.h"
#include "dbapserv.h"
#include "adslib.h"

void assocLines();
void addToModelSpace(AcDbObjectId&,AcDbEntity *);
void initApp(void);
void unloadApp(void);
extern "C"
AcRx::AppRetCode
acrxEntryPoint(AcRx::AppMsgCode,void *);

class MyObjectNotify:public AcDbObject
{
public:
 ACRX_DECLARE_MEMBERS(MyObjectNotify);
 MyObjectNotify(){};
 void eLinkage(AcDbObjectId i,double f=1.0)
 {
  mId=i;
  mFactor=f;
 };
 void modified(const AcDbObject *);
 Acad::ErrorStatus dwgInFields(AcDbDwgFiler *);
 Acad::ErrorStatus dwgOutFields(AcDbDwgFiler *)const ;
 Acad::ErrorStatus dxfInFields(AcDbDxfFiler *);
 Acad::ErrorStatus dxfOutFields(AcDbDxfFiler *) const;
private:
 AcDbObjectId mId;
 double mFactor;
};

ACRX_DXF_DEFINE_MEMBERS(MyObjectNotify,AcDbObject,
      AcDb::kDHL_CURRENT,AcDb::kMReleaseCurrent,
      0,MYOBJECTMODIFY,exam10b);

void MyObjectNotify::modified(const AcDbObject *pObj)
{
 AcDbLine *pLine=AcDbLine::cast(pObj);
 if(!pLine)
 {
  const char * str=pObj->isA()->name();
  acutPrintf("This is a %s.\n",str);
  acutPrintf("i only work with lines,sorry.\n");
  return;
 }
 acutPrintf("\nReactor attached to %lx calling %lx.\n",
  pLine->objectId(),mId);
 AcDbLine *pLine2;
 if(acdbOpenObject((AcDbObject *&)pLine2,mId,
  AcDb::kForWrite)==Acad::eOk)
 {
  AcGePoint3d p=pLine->startPoint();
  AcGePoint3d q=pLine->endPoint();
  AcGeVector3d v=q-p;
  double len=v.length();

  p=pLine2->startPoint();
  q=pLine2->endPoint();
  v=q-p;
  v=len*mFactor *v.normal();
  pLine2->setEndPoint(p+v);
  pLine2->close();
 }
}

Acad::ErrorStatus
MyObjectNotify::dwgInFields(AcDbDwgFiler * filer)
{
 assertWriteEnabled();
 AcDbObject::dwgInFields(filer);
 filer->readItem(&mFactor);
 filer->readItem((AcDbSoftPointerId *) &mId);
 return filer->filerStatus();
}


Acad::ErrorStatus
MyObjectNotify::dwgOutFields(AcDbDwgFiler * filer) const
{
 assertReadEnabled();
 AcDbObject::dwgOutFields(filer);
 filer->writeItem(mFactor);
 filer->writeItem((AcDbSoftPointerId *) &mId);
 return filer->filerStatus();
}


Acad::ErrorStatus
MyObjectNotify::dxfInFields(AcDbDxfFiler * filer)
{
 assertReadEnabled();
 Acad::ErrorStatus es;
 if((es=AcDbObject::dxfInFields(filer))!=Acad::eOk)
 {
  return es;
 }

 if(!filer->atSubclassData("MyObjectNotify"))
 {
  return Acad::eBadDxfSequence;
 }

 struct resbuf rbIn;
 while(es==Acad::eOk)
 {
  if((es=filer->readItem(&rbIn))==Acad::eOk)
  {
   if(rbIn.restype==AcDb::kDxfReal)
   {
    mFactor=rbIn.resval.rreal;
   }
   else if(rbIn.restype==AcDb::kDxfSoftPointerId)
   {
    acdbGetObjectId(mId,rbIn.resval.rlname);
   }
   else
   {
    return (filer->pushBackItem());
   }
  }
 }
 return filer->filerStatus();
}

Acad::ErrorStatus
MyObjectNotify::dxfOutFields(AcDbDxfFiler * filer) const
{
 assertReadEnabled();
 AcDbObject::dxfOutFields(filer);
 filer->writeItem(AcDb::kDxfSubclass,
  "MyObjectNotify");
 filer->writeItem(AcDb::kDxfReal,mFactor);
 filer->writeItem(AcDb::kDxfSoftPointerId,mId);
 return filer->filerStatus();
}

void assocLines()
{
 AcDbDatabase *pDb=acdbHostApplicationServices()->workingDatabase();
 AcDbObjectId aId,bId;
 AcDbLine *pLineA=new AcDbLine;
 pLineA->setDatabaseDefaults(pDb);
 pLineA->setStartPoint(AcGePoint3d(1,1,0));
 pLineA->setEndPoint(AcGePoint3d(2,1,0));
 addToModelSpace(aId,pLineA);
 ads_printf("Line A is %lx from 1,1 to 2,1j.\n",
  pLineA->objectId());
 AcDbLine *pLineB=new AcDbLine;
 pLineB->setDatabaseDefaults(pDb);
 pLineB->setStartPoint(AcGePoint3d(1,2,0));
 pLineB->setEndPoint(AcGePoint3d(2,2,0));
 addToModelSpace(bId,pLineB);
 ads_printf("Line B is %lx from 1,2 to 2,2.\n",
  pLineB->objectId());

 AcDbDictionary *pNamedObj;
 AcDbDictionary *pNameList;
 pDb->getNamedObjectsDictionary(pNamedObj,
  AcDb::kForWrite);
 if(pNamedObj->getAt("ASDK_DICT",
  (AcDbObject * &) pNameList,AcDb::kForWrite)
  ==Acad::eKeyNotFound)
 {
  pNameList=new AcDbDictionary;
  AcDbObjectId DictId;
  pNamedObj->setAt("ASDK_DICT",pNameList,DictId);
 }
 pNamedObj->close();
 MyObjectNotify *pObj=new MyObjectNotify();
 pObj->eLinkage(bId);
 AcDbObjectId objId;
 if((pNameList->getAt("object_to_notify_A",objId))
  ==Acad::eKeyNotFound)
 {
  pNameList->setAt("object_to_notify_A",pObj,objId);
  pObj->close();
 }
 else
 {
  delete pObj;
  ads_printf("object_to_notify_A already existes\n");
 }
 pLineA->addPersistentReactor(objId);
 pLineA->close();
 pObj=new MyObjectNotify();
 pObj->eLinkage(aId);
 if((pNameList->getAt("object_to_notify_B",objId))
  ==Acad::eKeyNotFound)
 {
  pNameList->setAt("object_to_notify_B",pObj,objId);
  pObj->close();
 }
 else
 {
  delete pObj;
  ads_printf("object_to_notify_b already existes\n");
 }
 pNameList->close();

 pLineB->addPersistentReactor(objId);
 pLineB->close();
}

void addToModelSpace(AcDbObjectId &objId,AcDbEntity * pEntity)
{
 AcDbBlockTable *pBlockTable;
 AcDbBlockTableRecord *pSpaceRecord;
 acdbHostApplicationServices()->workingDatabase()
  ->getSymbolTable(pBlockTable,AcDb::kForRead);
 pBlockTable->getAt(ACDB_MODEL_SPACE,pSpaceRecord,
  AcDb::kForWrite);
 pBlockTable->close();
 pSpaceRecord->appendAcDbEntity(objId,pEntity);
 pSpaceRecord->close();
 return;
}


void initApp()
{
 acedRegCmds->addCommand("EXAM10b",
  "ALINES",
  "ALINES",
  ACRX_CMD_MODAL,
  assocLines);
 MyObjectNotify::rxInit();
 acrxBuildClassHierarchy();
}

void unloadApp()
{
 acedRegCmds->removeGroup("EXAM10B");
 deleteAcRxClass(MyObjectNotify::desc());
}

AcRx::AppRetCode
acrxEntryPoint(AcRx::AppMsgCode msg, void *appId)
{
 switch(msg)
 {
 case AcRx::kInitAppMsg:
  acrxDynamicLinker->unlockApplication(appId);
  acrxDynamicLinker->registerAppMDIAware(appId);
  initApp();
  break;
 case AcRx::kUnloadAppMsg:
  unloadApp();
 }
 return AcRx::kRetOK;
}

 楼主| 发表于 2005-9-9 11:20:00 | 显示全部楼层

exam10b.def

LIBRARY  exam10b
DESCRIPTION  "EXAM10B APPLICATION"
EXPORTS
acrxEntryPoint  RIVATE
acrxGetApiVersion RIVATE

 楼主| 发表于 2005-9-9 11:23:00 | 显示全部楼层

用vc++6.0开发,autocad2002中使用正常。

command :ALINES

会增加两条直线,对任意一条直线操作,都会影响到第二个直线。

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

本版积分规则

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

GMT+8, 2024-11-26 04:42 , Processed in 0.154505 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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