明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1515|回复: 0

browse folder code .

[复制链接]
发表于 2003-11-26 11:38:00 | 显示全部楼层 |阅读模式
本帖最后由 作者 于 2003-11-26 12:35:41 编辑

// Date: Nov 18, 2003

#include <aced.h>
#include <string.h>
#include <rxregsvc.h>
#include <adscodes.h>
#include <dbsymtb.h>
#include <dbapserv.h>
#include <acutads.h>
// Header file for SHBrowseForFolder API function.
#include <windows.h>
#include <shlobj.h>

//#define MAX_PATH 500;;

int dofun();
int funcload();

// FUNCLOAD -- Define this application's external functions.
//                           Return RTERROR on error, else RTNORM.
static int funcload()
{
        if(!acedDefun("EXP_NEWLAYER", 0))
                return RTERROR;
        if(!acedDefun("EXP_BROWSEFOLDER", 1))
                return RTERROR;
}

void createNewLayer(char* lyrname, Adesk::UInt16 clr, AcDbObjectId ltypeId, Adesk::Boolean current)
{
        // We need to check if the layer name exists
        // If the layer name exists, apply the color
        // linetype id and whither to make it current
        // or not. In order to be current it cannot be
        // frozen, so we need to check for this also.
        // If the layer name does not exist we just create
        // a new layer with the properties contained in the arguments
       
        AcDbLayerTable *pLyrTable;
        AcDbLayerTableRecord *pLyrTblRecord;
        AcDbObjectId recId;

        AcCmColor color;
        color.setColorIndex(clr); // set color to parameter clr

        AcDbDatabase *pCurDb = NULL;

        pCurDb = acdbHostApplicationServices()->workingDatabase();


        pCurDb->getLayerTable(pLyrTable, AcDb::kForRead);
        // Check to see if the layer name exists
        if(pLyrTable->has(lyrname))
        {
                pLyrTable->getAt(lyrname, pLyrTblRecord, AcDb::kForWrite, Adesk::kFalse);
                // pLyrTblRecord now points at the layer table record
                // which was opened for write
                pLyrTblRecord->setIsFrozen(Adesk::kFalse);
                pLyrTblRecord->setColor(color);
                pLyrTblRecord->setLinetypeObjectId(ltypeId);

        }
        else
        {
                // Note how we can change the open mode
                // of the layer table from AcDb::kForRead
                // to AcDb::kForWrite

                pLyrTable->upgradeOpen();
                pLyrTblRecord = new AcDbLayerTableRecord;

                pLyrTblRecord->setName(lyrname);
                pLyrTblRecord->setColor(color);
                pLyrTblRecord->setLinetypeObjectId(ltypeId);

                pLyrTable->add(pLyrTblRecord);

        }

        // Get the layer Table ObjectId
        recId = pLyrTblRecord->objectId();

        pLyrTblRecord->close();
        pLyrTable->close();

        // Set the layer current if current
        // is equal to Adesk::kTrue
        // pCurDb is point to the current
        // drawing database
        // The database AcDbDatabase has a number of
        // query and edit functions for the header variables

        if(current)
        {
                pCurDb->setClayer(recId);
        }
}


// DOFUN -- Execute external function (called upon an RQSUBR request).
//          Return value from the function executed, RTNORM or RTERROR.
static int dofun()
{
        //char *lyrname = 0;
        char* lyrname = 0;
        int col = 0;
        char *ltype = 0;
        AcDbObjectId ltypeObjId;
    Adesk::Boolean current = 0;

        struct resbuf *rb;
        struct resbuf *rbTrav;
        struct resbuf *res_list;
        int val;
        int i = 0;

        // Declared variable for calling EXP_BROWSEFOLDER function
        char path[MAX_PATH];
        BROWSEINFO bi = {0};
        ITEMIDLIST *pidl;
        char *title = 0;

        // Get the function code and check that it's within range.
        if((val = acedGetFunCode()) < 0)
        {
                acdbFail("Receive nonexisted function code.");
                return RTERROR;
        }

        // Fetch the arguments, if any.
        rb = acedGetArgs();
        if(!rb)
        {
                acutPrintf("\nFailed to get arguments list. ");
                return RTERROR;
        }
        rbTrav = rb;

        switch(val)                // which function is called
        {
        case 0:
                while(rbTrav)
                {
                        switch(rbTrav->restype)
                        {
                        case RTSTR:
                                if(i == 0)
                                        lyrname = rbTrav->resval.rstring;
                                else if(i == 2)
                                        ltype = rbTrav->resval.rstring;
                                break;
                        case RTSHORT:
                                if(i == 1)
                                        col = rbTrav->resval.rint;
                                else if(i == 3)
                                        current = rbTrav->resval.rint;
                                break;
                        default:
                                break;
                        }        // switch
                        rbTrav = rbTrav->rbnext;
                        i += 1;
                }        // while

                if((lyrname == NULL))
                        lyrname = "NONE_NAME";

                if((ltype == NULL)||(strcmp(ltype, "") == 0))
                        ltype = "CONTINUOUS";

                AcDbLinetypeTable *pLinetypeTbl;
                acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLinetypeTbl, AcDb::kForRead);
                pLinetypeTbl->getAt(ltype,ltypeObjId);
                createNewLayer(lyrname,col,ltypeObjId,current);

                // The following code frament constructs a list to return to AutoLISP
                res_list = acutBuildList(
                        RTSTR, lyrname,
                        RTSHORT, col,
                        RTSTR, ltype,
                        RTSHORT, current,
                        0);
                if(res_list == NULL)
                {
                        acdbFail("Couldn't create list!\n");
                        return NULL;
                }
                acedRetList(res_list);
                if(res_list != NULL)
                        acutRelRb(res_list);
                break;
                // other function codes.
                //
        case 1:
                while(rbTrav)
                {
                        switch(rbTrav->restype)
                        {
                        case RTSTR:
                                if (i == 0)
                                {
                                        title = rbTrav->resval.rstring;
                                }
                                break;
                        default:
                                break;
                        }        // switch
                        rbTrav = rbTrav->rbnext;
                        i += 1;
                }        // while

                if( title != NULL)
                {
                        //bi.pszDisplayName = title;
                        bi.lpszTitle = title;
                        bi.ulFlags = BIF_RETURNONLYFSDIRS;
                }
                else
                {
                        //bi.pszDisplayName = title;
                        bi.lpszTitle = "Select a directory";
                        bi.ulFlags = BIF_RETURNONLYFSDIRS;
                }

                bi.lpfn = NULL;
                bi.lParam = 0;
                bi.iImage = 0;
                bi.hwndOwner = NULL;
                bi.pidlRoot = NULL;
        pidl = SHBrowseForFolder( &bi );

                if( pidl!= NULL)
                {
                        // 获取目录路径
                        SHGetPathFromIDList(pidl, path);
                        //const char * pPath=(char*)path;

                        //如何转换lpwstr宽字节,到一般的字符串。

                        //MessageBox(NULL,cString,"ok",MB_OK);

                        //释放内存

                        IMalloc *imalloc = 0;
                        if(SUCCEEDED(SHGetMalloc(&imalloc)))
                        {
                                imalloc->Free(pidl);
                                imalloc->Release();
                        }

                }
                if( path )
            acedRetStr(path);
                break;
                // Other function codes.
        default:
                break;
        }        // switch
        return 0;
}

// DLL Entry Point -- This function replaces main() for an ObjectARX program.
extern "C" AcRx::AppRetCode
acrxEntryPoint(AcRx::AppMsgCode msg, void* pkt)
{
        switch(msg)
        {
        case AcRx::kInitAppMsg:
                acrxDynamicLinker->unlockApplication(pkt);
                acrxRegisterAppMDIAware(pkt);
                break;
        case AcRx::kInvkSubrMsg:
                dofun();
                break;
        case AcRx::kLoadDwgMsg:
                funcload();
                break;
        default:
                break;
        }
        return AcRx::kRetOK;
}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

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

本版积分规则

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

GMT+8, 2025-2-22 19:19 , Processed in 0.164467 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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