기본 콘텐츠로 건너뛰기

Rectangle Jig Class

샘플 코드
// AsdkRectangleJig.h: interface for the AsdkRectangleJig class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_ASDKRECTANGLEJIG_H__B797B881_525A_491D_B14C_F2B9ACA711A8__INCLUDED_)
#define AFX_ASDKRECTANGLEJIG_H__B797B881_525A_491D_B14C_F2B9ACA711A8__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include
#include
#include     // asDblArray()
#include     // acdbWcs2Ecs() & acdbUcs2Ecs()
#include     // lwpoly stuff.
#include "DataMgr.h"
#include "ViewCreator.h"

//-----------------------------------------------------------------------------
//
class CRectInfo {
public:
    CRectInfo();   
    AcGePoint3d     m_topLeftCorner;    // First point selection.
    double          m_first;            // First Chamfer distance.
    double          m_second;           // Second Chamfer distance.
    double          m_bulge;            // Bulge value.
    double          m_elev;             // Elevation.
    double          m_thick;            // Thickness.
    double          m_width;            // Width.
    double          m_radius;           // Fillet radius.
    bool            m_cornerTreatment;  // Filleting or chamfering.
    bool            m_elevHandSet;
    // m_chamfDirUnitVec is the vector direction of the chamfer of the
    // top left corner of the rectangle. The chamfer opposite the top left
    // corner will have the same vector direction and the ones on the
    // alternate corners will be perpendicular to this one.
    //
    AcGeVector3d    m_chamfDirUnitVec;  
};

class AsdkRectangleJig : public AcEdJig  
{
public:
    AsdkRectangleJig(CViewCreatorDlg* pParent);
    virtual ~AsdkRectangleJig();
public:
    virtual DragStatus sampler();
    virtual Adesk::Boolean update();
    virtual AcDbEntity* entity() const;
    
    void    doRectangle();
    AcGePoint3d BottomLeftCorner();
    AcGePoint3d TopRightCorner();

    AcGePoint3d samplerCorner;
    static AcDbObjectId m_nObjectId;
private:
    CViewCreatorDlg* m_pParent;

    AcDbPolyline*   m_pLWPoly;
    AcGePoint3d     m_TopLeftCorner;
    AcGePoint3d     m_TopRightCorner;
    AcGePoint3d     m_BottomLeftCorner;
    AcGePoint3d     m_BottomRightCorner;
    AcGeVector3d    m_vecUnitZ;       // Unit vectors in the UCS plane
    AcGeVector3d    m_vecUnitX;
    AcGeVector3d    m_vecUnitY;
};
extern AsdkDataManager rectDataMgr; // MDI Safe
#define    plineInfo rectDataMgr.docData()
#endif // !defined(AFX_ASDKRECTANGLEJIG_H__B797B881_525A_491D_B14C_F2B9ACA711A8__INCLUDED_)

2. AsdkRectangleJig.cpp
// AsdkRectangleJig.cpp: implementation of the AsdkRectangleJig class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include
#include "resource.h"
#include "ECRS.h"
#include "AsdkRectangleJig.h"


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#define MAKEUCSCOORD(x, y)      tmpPoint = (y); \
acdbWcs2Ecs(asDblArray(tmpPoint), asDblArray(tmpPoint), asDblArray(m_vecUnitZ),\
Adesk::kFalse); x[X] = tmpPoint[X]; x[Y] = tmpPoint[Y];

AsdkDataManager rectDataMgr; // MDI Safe

// ----------------------------------------------------------------------------
// Constructor.
//
CRectInfo::CRectInfo()
{
    m_topLeftCorner = AcGePoint3d();
    m_first = m_second = m_bulge = m_elev = m_thick = m_width = m_radius = 0.0;
    m_cornerTreatment = FALSE;
    m_elevHandSet = FALSE;
}

AcDbObjectId AsdkRectangleJig::m_nObjectId;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//        parameter        :
//        description        :
//        remarks                :
//        returns                :
AsdkRectangleJig::AsdkRectangleJig(CViewCreatorDlg* pParent)
{
    m_pParent = pParent;

    m_pLWPoly = new AcDbPolyline();
    samplerCorner = AcGePoint3d();
    plineInfo.m_cornerTreatment = plineInfo.m_first != 0.0
        || plineInfo.m_second != 0.0
        || plineInfo.m_radius != 0.0;
   

    // Now need to get the current UCS Z-Axis to be used as the normal vector 
    // for the rectangle. At the same time, we get the x and y unit direction 
    // vectors used later.
    //
    if(CECRS::inPaperSpace()) {
        m_vecUnitX = acdbHostApplicationServices()->workingDatabase()->pucsxdir();
        m_vecUnitY = acdbHostApplicationServices()->workingDatabase()->pucsydir();
    } else {
        m_vecUnitX = acdbHostApplicationServices()->workingDatabase()->ucsxdir();
        m_vecUnitY = acdbHostApplicationServices()->workingDatabase()->ucsydir();
    }
    
    m_vecUnitZ = m_vecUnitX.crossProduct(m_vecUnitY);
   
    // Convert the incomming UCS point to ECS coordinate system
    //
    acdbUcs2Ecs(asDblArray(plineInfo.m_topLeftCorner), 
        asDblArray(m_TopLeftCorner), asDblArray(m_vecUnitZ), Adesk::kFalse);
    acdbUcs2Ecs(asDblArray(plineInfo.m_topLeftCorner), 
        asDblArray(plineInfo.m_topLeftCorner), asDblArray(m_vecUnitZ),
        Adesk::kFalse);
    AcGePoint2d initPoint;
    initPoint = AcGePoint2d(m_TopLeftCorner[X], m_TopLeftCorner[Y]);
    
    // If the user has set the elev option from the main command prompt,
    // then this will be the default until the user again sets it to 0.0.
    // If however the user simply picks a point with or without an object
    // snap, then use the Z value of the first point picked.
    //
    if (plineInfo.m_elevHandSet == TRUE)
        m_pLWPoly->setElevation(plineInfo.m_elev);
    else    m_pLWPoly->setElevation(m_TopLeftCorner[Z]);
    
    // If we are indeed filleting or chamfering the corners, then
    // we'll add the extra verticies here to have their bulges and
    // distances from the real corner changed on the fly.
    // 
    if (plineInfo.m_cornerTreatment == TRUE) {
        for (int i = 0 ; i < 8; i++)
            m_pLWPoly->addVertexAt(i, initPoint);
    } else {
        for (int i = 0 ; i < 4; i++)
            m_pLWPoly->addVertexAt(i, initPoint);
    }
    
    m_pLWPoly->setLineWeight(AcDb::kLnWt030);
    m_pLWPoly->setNormal(m_vecUnitZ);
    m_pLWPoly->setClosed(Adesk::kTrue);
    m_pLWPoly->setThickness(plineInfo.m_thick);
    m_pLWPoly->setConstantWidth(plineInfo.m_width);
    // Get the current default linetype scale
    m_pLWPoly->setLinetypeScale(acdbHostApplicationServices()
        ->workingDatabase()->celtscale());
    // Now for jig dragger purposes, convert the point back to world
    // coordinates.
    //
    acdbEcs2Wcs(asDblArray(m_TopLeftCorner), 
        asDblArray(m_TopLeftCorner), asDblArray(m_vecUnitZ), Adesk::kFalse);
    acdbEcs2Wcs(asDblArray(plineInfo.m_topLeftCorner), 
        asDblArray(plineInfo.m_topLeftCorner), asDblArray(m_vecUnitZ),
        Adesk::kFalse);
}

AsdkRectangleJig::~AsdkRectangleJig()
{

}

//-----------------------------------------------------------------------------
// This function creates an AcDbPolyline object after the
// jig startes and aquires the necessary vertex for the opposite
// corner from the drag samples.
//
void AsdkRectangleJig::doRectangle()
{
    AcEdJig::DragStatus stat;
    
    setDispPrompt("\nOther corner: ");
    // Get the other corner now.
    //
    stat = drag();
    
    // Now add the polyline rectangle to the database's current space
    // if we return a kNormal stat from drag(). If we don't then delete
    // the polyline.
    //
        AsdkRectangleJig::m_nObjectId = AcDbObjectId::kNull;
    if (stat == kNormal){
        AsdkRectangleJig::m_nObjectId = append();
        m_pLWPoly->close();
    }else    delete m_pLWPoly;
    
    setDispPrompt("\n");
}

//-----------------------------------------------------------------------------
// This function is called by the drag function in order to
// aquire a sample input
//
AcEdJig::DragStatus AsdkRectangleJig::sampler()
{
    DragStatus stat = kNormal;
    
    setUserInputControls((UserInputControls)
        (   AcEdJig::kNoNegativeResponseAccepted
        |  AcEdJig::kNoZeroResponseAccepted)
        );
    
    setSpecialCursorType(kCrosshair);
    
    stat = acquirePoint(m_BottomRightCorner, plineInfo.m_topLeftCorner);  
    
    // Now project the point at the crosshairs to the ECS of the 
    // polyline being drawn.
    //
    AcGePlane planeParallelToUCS(m_TopLeftCorner, m_vecUnitZ);    
    m_BottomRightCorner = m_BottomRightCorner.project(planeParallelToUCS, m_vecUnitZ);
    
    // If the newly acquired point is the same as it was
    // in the last sample, then we return kNoChange so that
    // AsdkRectangleJig::update() will not be called and the
    // last update call will be able to finish thus allowing
    // the polyline to fully elaborate.
    //
    if (samplerCorner != m_BottomRightCorner) {
        // m_BottomRightCorner will be used to update the remaining corners
        // in AsdkRectangleJig::update() below.
        //
        samplerCorner = m_BottomRightCorner;
    } else if (stat == AcEdJig::kNormal)
        return AcEdJig::kNoChange;
    
    return stat;
}

//-----------------------------------------------------------------------------
// This function is called to update the entity based on the
// input values
//
Adesk::Boolean AsdkRectangleJig::update()
{
    AcGePoint2d adjustedPoint;
    AcGePoint3d tmpPoint;       // Used by MAKEUCSCOORD macro.
    
    // We'll use the AcGeLine::intersectWith() function to infer the
    // remaining points.
    //
    AcGeLine3d lineX, lineY;
    lineX.set(m_TopLeftCorner, m_vecUnitX);
    lineY.set(m_BottomRightCorner, m_vecUnitY);
    // Top right corner is intersection of lineX and lineY.
    //
    lineX.intersectWith(lineY, m_TopRightCorner);
    
    lineX.set(m_BottomRightCorner, m_vecUnitX);
    lineY.set(m_TopLeftCorner, m_vecUnitY);
    // Bottom left corner is intersection of lineX and lineY.
    //
    lineX.intersectWith(lineY, m_BottomLeftCorner);
    
    AcGeVector3d tmpXVec, tmpYVec;
    // Check to see if we have flipped around the X or Y axis.
    //
    bool bXFlip = m_vecUnitX.dotProduct(m_TopLeftCorner - m_TopRightCorner)  >0;
    bool bYFlip = m_vecUnitY.dotProduct(m_TopLeftCorner - m_BottomLeftCorner)<0; 
    
    // If the rectangle is dragged into the first or third quadrant,
    // we need to reverse the sign of the bulge as well as reverse
    // the x and y direction vectors.
    //
    tmpXVec = bXFlip ? -1 * m_vecUnitX : m_vecUnitX;
    tmpYVec = bYFlip ? -1 * m_vecUnitY : m_vecUnitY;
    
    // Now update the polyline with the latest setting
    //
    if (plineInfo.m_cornerTreatment) {
        // We are going to fillet of chamfer this polyline rectangle. As such,
        // the constructor has added the extra points at the corners to allow
        // for there placement and bulge values to be updated on the fly.
        // If, during the dragging, the rectangle is still too small to show the
        // given radius or chamfer edges, the we will put the extra points in the 
        // corners and set the bulges to 0.0, so the rectangle retains its 
        // square corners until the user stretches the rectangle to a size large
        // enough to have the corner treatment displayed.
        //
        
        // Use temporaries to see if we're too small to show fillet/chamfer, so
        // we don't need to convert back to world.
        // 
        AcGePoint2d point_TL, point_TR, point_BL;
        MAKEUCSCOORD(point_TL, m_TopLeftCorner);
        MAKEUCSCOORD(point_TR, m_TopRightCorner);
        MAKEUCSCOORD(point_BL, m_BottomLeftCorner);
        
        bool tooSmall = (point_TL.distanceTo(point_TR)
            < plineInfo.m_first + plineInfo.m_second)
            || (point_TL.distanceTo(point_BL)
            < plineInfo.m_first + plineInfo.m_second);
        if (tooSmall) {
            // Still to small to show the corner treatment.
            //
            m_pLWPoly->setBulgeAt(0, 0.0);
            MAKEUCSCOORD(adjustedPoint, m_TopLeftCorner);
            m_pLWPoly->setPointAt(0, adjustedPoint);
            m_pLWPoly->setPointAt(1, adjustedPoint);
            

           m_pLWPoly->setBulgeAt(2, 0.0);  
            MAKEUCSCOORD(adjustedPoint, m_TopRightCorner);
            m_pLWPoly->setPointAt(2, adjustedPoint);
            m_pLWPoly->setPointAt(3, adjustedPoint);
            
            m_pLWPoly->setBulgeAt(4, 0.0);  
            MAKEUCSCOORD(adjustedPoint, m_BottomRightCorner);
            m_pLWPoly->setPointAt(4, adjustedPoint);
            m_pLWPoly->setPointAt(5, adjustedPoint);
            
            m_pLWPoly->setBulgeAt(6, 0.0);  
            MAKEUCSCOORD(adjustedPoint, m_BottomLeftCorner);
            m_pLWPoly->setPointAt(6, adjustedPoint);
            m_pLWPoly->setPointAt(7, adjustedPoint);
        } else {
            double tmpBulge;
            
            tmpBulge = ((!bXFlip && !bYFlip) || (bXFlip && bYFlip))
                ? plineInfo.m_bulge : -plineInfo.m_bulge;
            
            // Now we will set adjustedPoint to the intersection of the rectangle
            // sides with the place where the new end points will be.
            //
            m_pLWPoly->setBulgeAt(0, tmpBulge);
            MAKEUCSCOORD(adjustedPoint,
                m_TopLeftCorner + (-plineInfo.m_first * tmpYVec));
            m_pLWPoly->setPointAt(0, adjustedPoint);
            
            MAKEUCSCOORD(adjustedPoint,
                m_TopLeftCorner + plineInfo.m_second * tmpXVec);
            m_pLWPoly->setPointAt(1, adjustedPoint);
            
            m_pLWPoly->setBulgeAt(2, tmpBulge); 
            MAKEUCSCOORD(adjustedPoint,
                m_TopRightCorner + (-plineInfo.m_first * tmpXVec));
            m_pLWPoly->setPointAt(2, adjustedPoint);
            
            MAKEUCSCOORD(adjustedPoint,
                m_TopRightCorner + (-plineInfo.m_second * tmpYVec));
            m_pLWPoly->setPointAt(3, adjustedPoint);
           
            m_pLWPoly->setBulgeAt(4, tmpBulge);
            MAKEUCSCOORD(adjustedPoint,
                m_BottomRightCorner + plineInfo.m_first * tmpYVec);
            m_pLWPoly->setPointAt(4, adjustedPoint);
            
            MAKEUCSCOORD(adjustedPoint,
                m_BottomRightCorner + (-plineInfo.m_second * tmpXVec));
            m_pLWPoly->setPointAt(5, adjustedPoint);
            
           m_pLWPoly->setBulgeAt(6, tmpBulge);
            MAKEUCSCOORD(adjustedPoint,
                m_BottomLeftCorner + plineInfo.m_first * tmpXVec);
            m_pLWPoly->setPointAt(6, adjustedPoint);
            
            MAKEUCSCOORD(adjustedPoint,
                m_BottomLeftCorner + plineInfo.m_second * tmpYVec);
            m_pLWPoly->setPointAt(7, AcGePoint2d(adjustedPoint[X],
                adjustedPoint[Y]));
        }
    } else {
        // If this polyline is not having its corners treated, ie chamfered, or 
        // filleted then simply update the corners. Since we knew this ahead of 
        // time, the constructor did not add any extra verticies at the corners.
        //
        MAKEUCSCOORD(adjustedPoint, m_TopLeftCorner);
        m_pLWPoly->setPointAt(0, adjustedPoint);
        
        MAKEUCSCOORD(adjustedPoint, m_TopRightCorner);
        m_pLWPoly->setPointAt(1, adjustedPoint);
        
        MAKEUCSCOORD(adjustedPoint, m_BottomRightCorner);
        m_pLWPoly->setPointAt(2, adjustedPoint);
        
        MAKEUCSCOORD(adjustedPoint, m_BottomLeftCorner);
        m_pLWPoly->setPointAt(3, adjustedPoint);
    } 
    return Adesk::kTrue;
}

//-----------------------------------------------------------------------------
// This function must be implemented to return a pointer to
// the entity being manipulated by the jig.
//
AcDbEntity* AsdkRectangleJig::entity() const
{
    return m_pLWPoly;
}

//        parameter        :
//        description        :
//        remarks                :
//        returns                :
AcGePoint3d AsdkRectangleJig::BottomLeftCorner(){
    AcGePoint3d ptBottomLeft=m_BottomLeftCorner;

    ptBottomLeft.x = (m_BottomRightCorner.x < ptBottomLeft.x) ? m_BottomRightCorner.x : tBottomLeft.x;
    ptBottomLeft.y = (m_TopLeftCorner.y < ptBottomLeft.y) ? m_TopLeftCorner.y : tBottomLeft.y;

    return ptBottomLeft;
}

//        parameter        :
//        description        :
//        remarks                :
//        returns                :
AcGePoint3d AsdkRectangleJig::TopRightCorner(){
    AcGePoint3d ptTopRight=m_TopRightCorner;

   ptTopRight.x = (m_TopLeftCorner.x > ptTopRight.x) ? m_TopLeftCorner.x : ptTopRight.x;
    ptTopRight.y = (m_BottomRightCorner.y > ptTopRight.y) ? m_BottomRightCorner.y : tTopRight.y;

    return ptTopRight;
}

댓글

이 블로그의 인기 게시물

80040154 오류로 인해 CLSID가 {xxxx-...}인 구성 요소의 COM 클래스 팩터리를 검색하지 못했습니다.

원문보기 .NET 으로 만든 응용프로그램에서 com 객체를 호출한 경우 Windows7 64bit 에서 제목과 같은 에러가 발생했다. Win32 COM 과 .NET 프로그램간의 호환성 때문에 생긴 문제였다. 원인은 .NET 실행시 JIT 컴파일러에 의해 최적화된 기계어로 변환되기 때문.. Win32 COM은 컴파일시.. Win32 COM에 맞춰 빌드 속성에서 하위버전으로 맞춰 컴파일을 다시하는 방법도 있지만 메인 프로젝트가 .NET이라면 참조되는 모든 프로젝트를 다 바꿔야할 노릇.. 또 다른 방법은 COM+를 이용하여 독립적으로 만드는 것이다. 분리시키는 방법은 아래 주소해서 확인할 수 있다. http://support.microsoft.com/kb/281335 나의 경우는 Win32 COM DLL을 64비트 .NET 프로그램에서 참조하니 COM 객체를 제대로 호출하지 못하였습니다. 그래서 .NET 프로그램의 Target Machine을 x86으로 설정하니 제대로 COM 객체를 호출하였습니다.

[Pyinstaller] 실행 파일 관리자 권한 획득하기

고객사에서 일부 사용자에게서 프로그램 오류가 발생한다며 아래와 같이 에러 캡처를 보내왔습니다. 프로그램에서 로그를 남기기 위해 로그 파일을 생성하는데 권한의 문제로 로그 파일을 생성하지 못해 프로그램 오류가 발생한 것 같습니다. 처음에는 Python 코드에서 관리자 권한을 요청하는 코드를 넣으려고 했는데, 실제로 Stackoverflow를 찾아보면 이런 내용이 나옵니다. 프로그램이 관리자 권한으로 실행되지 않았다면 관리자 권한으로 다시 프로그램을 실행시키는 코드입니다. import os import sys import win32com.shell.shell as shell ASADMIN = 'asadmin' if sys.argv[-1] != ASADMIN: script = os.path.abspath(sys.argv[0]) params = ' '.join([script] + sys.argv[1:] + [ASADMIN]) shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params) sys.exit(0) 하지만 개인적으로 이런 방식은 마음에 들지 않았고 조금 더 찾아보니 Pyinstaller로 exe 파일을 만들 때 옵션을 설정하여 관리자 권한을 요청하도록 할 수 있다고 합니다. --uac-admin을 옵션에 추가하면 프로그램 실행 시 관리자 권한을 요청할 수 있습니다. pyinstaller.exe --uac-admin sample.py 하지만 안타깝게도 이 방식은 원하는 대로 동작하지 않았습니다. 마지막으로 manifest 파일을 이용하여 시도해보았습니다. spec 파일을 이용하여 pyinstaller로 빌드하면 <실행 파일 이름>.manifest 라는 파일이 생성됩니다. 파일에서 아랫부분을 찾아볼 수 있습니다. <security> <re

초간단 프로그램 락 걸기

프로그램에 락을 걸 일이 생겨났다. 하드웨어 락을 걸면 쉬울텐데 그 정도는 아니고 프로그램의 실행 날짜를 제한 해 달라고 한다. 그래서 파일(license.lic)을 가지고 락을 걸리고 결정을 했다. 요구 사항은 아래와 같다. 1. license.lic 파일이 없으면 프로그램을 실행 할수 없게 한다. 2. 지정한 날짜를 넘어서는 프로그램을 실행 할수 없게 한다. 3. 사용자가 시스템 날짜를 되돌렸을때 인식하여 프로그램을 실행 할수 없게 한다. 음.... 1.번 문제는 사용자가 프로그램을 실행하기 위해서 license.lic 파일을 받아야만 한다. license.lic 파일에는 최근 실행 날짜/종료날짜 이런식으로 적도록 한다.(물론 내용은 암호화 한다.) 최근 실행날짜는 프로그램이 실행때마다 업데이트 하도록 하고 시스템 날짜와 비교하여 시스템 날짜가 최근 실행 날짜보다 이전의 날짜면 시스템 날짜를 되돌렸다고 인식하도록 한다.(3.번 문제 해결) 시스템 날짜와 종료 날짜를 비교하여 시스템 날짜가 종료 날짜를 넘으면 프로그램을 실행 할수 없도록 한다.(2.번 문제 해결)