设为首页收藏本站

晓东CAD家园-论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 35|回复: 0

[布局] 创建图纸空间视口

[复制链接]

已领礼包: 40个

财富等级: 招财进宝

发表于 2021-1-7 19:52:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

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

x
问题:
How can I create a new viewport in paper space and activate it? I want to let
the user specify a rectangle (like the MVIEW command) and an existing view name.
With this data I want to create a new viewport.

解答:
To create a new viewport in paper space, perform the following steps:

1. Create a new object of type AcDbViewport.
2. Set the view coordinates (with setWidth(), setHeight() and setCenterPoint()).
3. Append the new viewport to the paper space.
4. Get the view the user specified (AcDbViewTableRecord).
5. Set this view on the new viewport with acdbSetCurrentView().
6. Activate the viewport with AcDbViewport::setOn().

NOTE: The AcDbViewport::setOn() function only works if your command has been
registered without the ACRX_CMD_TRANSPARENT flag. If this flag is set,
AcDbViewport::setOn() returns with eCommandWasInProgress and you cannot activate
the viewport until you set tilemode to 1 and back to 0.

The following code demonstrates how to do this:

  1. void fCreateViewPort()
  2. {
  3.   // Only works in paperspace
  4.   AcDbObjectId mCurViewportId = acedGetCurViewportObjectId();


  5.   if (mCurViewportId == AcDbObjectId::kNull)
  6.   {
  7.     acutPrintf("\nCommand only works in paperspace.");
  8.     return;
  9.   }


  10.   AcDbViewport *pCurViewport;
  11.   if (Acad::eOk != acdbOpenObject(pCurViewport,mCurViewportId,AcDb::kForRead))
  12.   {
  13.     acutPrintf("\nCannot get active viewport.");
  14.     return;
  15.   }


  16.   if (pCurViewport->number() != 1)
  17.   {
  18.     acutPrintf("\nCommand only works in paperspace.");
  19.     pCurViewport->close();
  20.     return;
  21.   }
  22.   pCurViewport->close();


  23.   // Ask for the position
  24.   ads_point pt1,pt2;


  25.   if (RTNORM != acedGetPoint(NULL, "\nSelect first corner: ", pt1))
  26.     return;


  27.   if (RTNORM != acedGetCorner(pt1, "\nSelect second corner: ", pt2))
  28.     return;


  29.   // Ask for the view to use
  30.   char mViewName[133];


  31.   if (RTNORM != acedGetString(0, "\nEnter name of view to use: ", mViewName))
  32.     return;



  33.   // Create new viewport


  34.   AcDbViewport *pViewport = new AcDbViewport;


  35.   pViewport->setWidth(fabs(pt2[X] - pt1[X]));
  36.   pViewport->setHeight(fabs(pt2[Y] - pt1[Y]));
  37.   pViewport->setCenterPoint(AcGePoint3d(pt1[X] + (pt2[X] - pt1[X]) / 2,
  38.       pt1[Y] + (pt2[Y] - pt1[Y]) / 2,
  39.       pt1[Z]));


  40.   // Append new viewport to paper space
  41.   AcDbBlockTable *pTable;
  42.   AcDbBlockTableRecord *pPsBTR;


  43.   if (Acad::eOk != acdbHostApplicationServices()->workingDatabase()->getBlockTable(pTable, AcDb::kForRead))
  44.   {
  45.     acutPrintf("\nCannot get block table.");
  46.     delete pViewport;
  47.     return;
  48.   }


  49.   if (Acad::eOk != pTable->getAt(ACDB_PAPER_SPACE, pPsBTR, AcDb::kForWrite))
  50.   {
  51.     acutPrintf("\nCannot access paper space.");
  52.     pTable->close();
  53.     delete pViewport;
  54.     return;
  55.   }
  56.   pTable->close();


  57.   AcDbObjectId mViewPortId;
  58.   if (Acad::eOk != pPsBTR->appendAcDbEntity(mViewPortId, pViewport))
  59.   {
  60.     acutPrintf("\nCannot append viewport to paper space.");
  61.     pPsBTR->close();
  62.     delete pViewport;
  63.     return;
  64.   }

  65.   pPsBTR->close();
  66.   pViewport->setOn();

  67.   // Set the view
  68.   AcDbViewTable *pViewTable;
  69.   AcDbViewTableRecord *pViewTR;

  70.   if (Acad::eOk != acdbHostApplicationServices()->workingDatabase()->getViewTable(pViewTable, AcDb::kForRead))
  71.   {
  72.     acutPrintf("\nCannot get view table.");
  73.     pViewport->close();
  74.     return;
  75.   }
  76.   if (Acad::eOk != pViewTable->getAt(mViewName, pViewTR, AcDb::kForRead))
  77.   {
  78.     acutPrintf("\nCannot access view '%s'.", mViewName);
  79.     pViewport->close();
  80.     pViewTable->close();
  81.     return;
  82.   }
  83.   pViewTable->close();
  84.   if (acedSetCurrentView(pViewTR, pViewport)!=Acad::eOk)
  85.    acutPrintf("\nFailed to set view");
  86.   // Close the objects
  87.   pViewTR->close();
  88.   pViewport->close();
  89. }


论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|小黑屋|手机版|Archiver|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )  

GMT+8, 2021-1-22 08:30 , Processed in 0.098338 second(s), 12 queries , Gzip On, WinCache On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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