CAD二次开发学习笔记六(用类来组织函数)

新建ObjectARX项目CreateEnts

打开类视图,右击->新建类

这个类用来保存创建实体的函数。

包含#include "StdAfx.h"

代码如下:

头文件
#pragma once

#include 
"StdAfx.h"

class CCreateEnt
{
public:
    CCreateEnt(
void);
    
~CCreateEnt(void);
    
static AcDbObjectId CreateLine(AcGePoint3d ptStart,AcGePoint3d ptEnd);
    
static AcDbObjectId PostToModelSpace(AcDbEntity* pEnt);
    
static AcDbObjectId CreateCircle(AcGePoint3d ptCenter,AcGeVector3d vec,double radius);
};

 

 

源文件
#include "StdAfx.h"
#include 
"CreateEnt.h"

CCreateEnt::CCreateEnt(
void)
{
}

CCreateEnt::
~CCreateEnt(void)
{
}

//将实体添加到图形数据库的模型空间
AcDbObjectId CCreateEnt::PostToModelSpace(AcDbEntity* pEnt)
{
    AcDbBlockTable 
*pBlockTable;
    acdbHostApplicationServices()
->workingDatabase()->getBlockTable(pBlockTable,AcDb::kForRead);

    AcDbBlockTableRecord 
*pBlockTableRecord;
    pBlockTable
->getAt(ACDB_MODEL_SPACE,pBlockTableRecord,AcDb::kForWrite);

    AcDbObjectId entId;
    pBlockTableRecord
->appendAcDbEntity(entId,pEnt);
    pBlockTable
->close();
    pBlockTableRecord
->close();
    pEnt
->close();

    
return entId;
}
//创建直线
AcDbObjectId CCreateEnt::CreateLine(AcGePoint3d ptStart,AcGePoint3d ptEnd)
{
    AcDbLine 
*pLine = new AcDbLine(ptStart,ptEnd);
    
//将实体添加到图形数据库
    AcDbObjectId lineId;
    lineId 
= CCreateEnt::PostToModelSpace(pLine);

    
return lineId;
}

//创建圆
AcDbObjectId CCreateEnt::CreateCircle(AcGePoint3d ptCenter,AcGeVector3d vec,double radius)
{
    AcDbCircle 
*pCircle = new AcDbCircle(ptCenter,vec,radius);
    
//将实体添加到图形数据库
    AcDbObjectId circleId;
    circleId 
= CCreateEnt::PostToModelSpace(pCircle);

    
return circleId;
}

 


同样添加一个ModifyEnt类用来修改实体。

头文件
#pragma once

#include 
"StdAfx.h"

class CModifyEnt
{
public:
    CModifyEnt(
void);
    
~CModifyEnt(void);
    
static Acad::ErrorStatus ChangeColor(AcDbObjectId entId,Adesk::UInt16 colorIndex);
    
static Acad::ErrorStatus ChangeLayer(AcDbObjectId entId,CString strLayerName);
};

 

 

源文件
#include "StdAfx.h"
#include 
"ModifyEnt.h"

CModifyEnt::CModifyEnt(
void)
{
}

CModifyEnt::
~CModifyEnt(void)
{
}

//修改实体颜色
Acad::ErrorStatus CModifyEnt::ChangeColor(AcDbObjectId entId,Adesk::UInt16 colorIndex)
{
    AcDbEntity 
*pEntity;
    
//打开图形数据库中的对象
    acdbOpenObject(pEntity,entId,AcDb::kForWrite);
    
//修改实体的颜色
    pEntity->setColorIndex(colorIndex);
    
//用完之后,及时关闭
    pEntity->close();

    
return Acad::eOk;
}
//改变实体所在层
Acad::ErrorStatus CModifyEnt::ChangeLayer(AcDbObjectId entId,CString strLayerName)
{
    AcDbEntity 
*pEntity;
    
//打开图形数据库中的对象
    acdbOpenObject(pEntity,entId,AcDb::kForWrite);
    
//修改实体的图层
    pEntity->setLayer(strLayerName);
    
//用完之后,及时关闭
    pEntity->close();

    
return Acad::eOk;
}

 

 acrxEntryPoint.cpp中包含CreateEnt.hModifyEnt.h

大气象
// (C) Copyright 2002-2005 by Autodesk, Inc. 
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted, 
// provided that the above copyright notice appears in all copies and 
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting 
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS. 
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK, INC. 
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to 
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Comp9uter Software), as applicable.
//

//-----------------------------------------------------------------------------
//----- acrxEntryPoint.h
//-----------------------------------------------------------------------------
#include "StdAfx.h"
#include 
"resource.h"

#include 
"CreateEnt.h"
#include 
"ModifyEnt.h"

//-----------------------------------------------------------------------------
#define szRDS _RXST("")

//-----------------------------------------------------------------------------
//----- ObjectARX EntryPoint
class CCreateEntsApp : public AcRxArxApp {

public:
    CCreateEntsApp () : AcRxArxApp () {}

    
virtual AcRx::AppRetCode On_kInitAppMsg (void *pkt) {
        
// TODO: Load dependencies here

        
// You *must* call On_kInitAppMsg here
        AcRx::AppRetCode retCode =AcRxArxApp::On_kInitAppMsg (pkt) ;
        
        
// TODO: Add your initialization code here

        
return (retCode) ;
    }

    
virtual AcRx::AppRetCode On_kUnloadAppMsg (void *pkt) {
        
// TODO: Add your code here

        
// You *must* call On_kUnloadAppMsg here
        AcRx::AppRetCode retCode =AcRxArxApp::On_kUnloadAppMsg (pkt) ;

        
// TODO: Unload dependencies here

        
return (retCode) ;
    }

    
virtual void RegisterServerComponents () {
    }


    
// - CreateEnts.ChangeColor command (do not rename)
    static void CreateEntsChangeColor(void)
    {
        
// Add your code for command CreateEnts.ChangeColor here
        
//创建直线
        AcGePoint3d ptStart(0,0,0);
        AcGePoint3d ptEnd(
100,100,0);
        AcDbObjectId lineId;
        lineId 
= CCreateEnt::CreateLine(ptStart,ptEnd);
        
//修改直线颜色
        Adesk::UInt16 nColor = 1;
        CModifyEnt::ChangeColor(lineId,nColor);
    }
} ;

//-----------------------------------------------------------------------------
IMPLEMENT_ARX_ENTRYPOINT(CCreateEntsApp)

ACED_ARXCOMMAND_ENTRY_AUTO(CCreateEntsApp, CreateEnts, ChangeColor, MyCommand1, ACRX_CMD_TRANSPARENT, NULL)

 

 

posted @ 2011-02-21 13:18  大气象  阅读(2459)  评论(0编辑  收藏  举报
http://www.tianqiweiqi.com