使用机械图层和图层组(第 1 部分,共 2 部分)

概述

Mechanical SDK 预配置为在特定图层上创建对象。无论当前设置的是哪个图层,Mechanical SDK 都只在预定义图层上创建对象。如果图层不存在,则会自动创建。这些机械图层具有预定义设置,例如图层颜色、线宽等,这些设置取自一组预配置设置,称为图层定义。

您可以创建多组机械图层。每组称为一个图层组。使用机械图层组,您可以:

  • 将对象从一个图层组移动或复制到另一个图层组。
  • 全局处理属于图层组的各个图层。例如,无论您在哪个图层组上创建它,都可以更改线宽。
  • 为图层组设置颜色,然后通过图层组颜色覆盖单个图层的颜色。
  • 锁定和冻结图层组以简化设计工作。

有三个文件声明了所需的类和函数:

  • AmiLayer.h
  • AmiLayerCfg.h
  • AmiLayObjCfg.h

这些文件属于 McadApi 模块,可在公共 Include 目录中找到。

使用图层的示例

您可以创建以下任何机械图层:AM_0、AM_1、AM_2、AM_3、AM_4、AM_5、AM_6、AM_7、AM_8、AM_9、AM_10、AM_11、AM_12、AM_0N、AM_1N、AM_2N、AM_3N、AM_4N、AM_5N、AM_6N、AM_7N、AM_8N、AM_9N、AM_10N、AM_11N、AM_12N、AM_CL、AM_PAREF、AM_BOR、AM_VIEWS、AM_INV、AM_TR、AM_HID、AM_VIS、AM_PARDIM、AM_REFDIM。

这些图层包含一组预配置参数,并根据这些参数创建。使用列表中的名称创建机械图层,否则将创建常规图层。

要创建新的机械图层,请使用以下代码示例:

// Note: The next block of code requires additional inclusion of "AmiLayer.h"

OdDbDatabase* pDb = ... // get a pointer to the current database

OdString layerName = L"AM_11"; // layer name from the list above to create
OdString layerGroup; // group name can be empty, in this case no group is
                     // created. If you want to create a layer in a specific
                     // group, then specify the name of the group

AmiStatus status = amiCreateLayer(layerName, layerGroup, pDb);
if (status.success())
{
  ... // The layer is successfully created
}
else
{
  ... // The layer can not be created. Perhaps it already exists or input
      // parameters are not set correctly
}

要从文件导入图层配置,请使用以下代码示例:

// Note: The next block of code requires additional inclusion of "AmiLayer.h"

OdDbDatabase* pDb = ... // get a pointer to the current database

bool bReplace = true; // for a complete copy of mechanical layer settings
                      // if set as false, only part of settings will be copied
OdString dwgName = ... // set file path with file name for settings import

AmiStatus status = amiImportLayCfg(pDb, dwgName, bReplace);
if (status.success())
{
  ... // Settings successfully imported
}
else
{
  ... // Failure during import
}

要将所有与机械图层相关的设置重置为出厂默认值,请使用以下代码示例:

//Note: The next block of code requires additional inclusion of "AmiLayer.h"

OdDbDatabase* pDb = ... // get a pointer to the current database

AmiStatus status = amiSetLayCfgDefault(pDb);
if (status.success())
{
  ... // Settings successfully restored to defaults
}
else
{ 
  ... // Failure during settings reset
}

您可以使用以下代码示例获取指定标准的所有机械图层数组:

// Note: The next block of code requires additional inclusion of "AmiLayer.h" and
//       "AcmStandardManager.h"

OdDbDatabase* pDb = ... // get a pointer to the current database

// get the current std ID
OdDbObjectId stdId;
if (getAcmStdMgr()->getCurrent(stdId, pDb) != Acm::eOk)
  return;

OdArray<AmiLayerCfg*> arLayCfg;
AmiStatus status = amiGetAllLayers(arLayCfg, stdId);
if (status.success())
{
  // Perform some actions with the array. For example you can loop through all
  // elements of the array and set the same lineweight value. In this case don't
  // forget to add the "AmiLayerCfg.h" inclusion

  for (OdUInt16 i = 0; i < arLayCfg.length(); ++i)
  {
    AmiLayerCfg* pLayerCfg = arLayCfg[i];
    if (pLayerCfg)
      pLayerCfg->setLineweight(OdDb::kLnWt005);
  }
}
else
{
  ... // Failure during execution
}

// Don't forget to clear the memory because the ‘new’ operator is used to create
// each element in the array

for (OdUInt16 i = 0; i < arLayCfg.length(); ++i)
  delete arLayCfg[i];

要仅更改一个机械图层的值,无需获取整个数组并在其中查找特定对象。您可以使用以下示例代码完成此操作:

// Note: The next block of code requires additional inclusion of "AmiLayer.h",
//       "AcmStandardManager.h" and "AmiLayerCfg.h"

OdDbDatabase* pDb = ... // get a pointer to the current database

// get the current std ID
OdDbObjectId stdId;
if (getAcmStdMgr()->getCurrent(stdId, pDb) != Acm::eOk)
  return;

OdString layerName = ... // set the layer name from the previously mentioned
                         // list of names for the amiCreateLayer() function
AmiLayerCfg* layerProp = NULL;

AmiStatus status = amiGetLayerProperty(layerName, layerProp, stdId);
if (status.success())
{
  layerProp->setInvisible(true);
}
else
{
  ... // Failure during execution. Perhaps the input parameters are incorrect
}

// Don't forget to clear the memory because the ‘new’ operator was used for
// object creation

if (layerProp)
  delete layerProp;

要获取指定标准的所有机械图层对象的数组,请使用以下代码示例:

// Note: The next block of code requires additional inclusion of "AmiLayer.h" and
//       "AcmStandardManager.h"

OdDbDatabase* pDb = ... // get a pointer to the current database

// get the current std ID for amiGetAllLayerObjs
OdDbObjectId stdId;
if (getAcmStdMgr()->getCurrent(stdId, pDb) != Acm::eOk)
  return;

OdArray<AmiLayObjCfg*> arLayObjCfg;
AmiStatus status = amiGetAllLayerObjs(arLayObjCfg, stdId);
if (status.success())
{
  // Perform an action with the array. For example you can loop through all
  // elements of the array and set the same line weight value. In this case
  // don't forget to add the "AmiLayObjCfg.h" inclusion

  for (OdUInt16 i = 0; i < arLayObjCfg.length(); ++i)
  {
    AmiLayObjCfg* pLayObjCfg = arLayObjCfg[i];
    if (pLayObjCfg)
      pLayObjCfg->setLineweight(OdDb::kLnWt005);
  }
}
else
{
  ... // Failure during execution
}

// Don't forget to clear the memory because the ‘new’ operator is used to create
// each element in the array

for (OdUInt16 i = 0; i < arLayObjCfg.length(); ++i)
  delete arLayObjCfg[i];

要仅更改一个机械图层对象的值(就像机械图层示例中一样),无需获取整个数组并在其中查找特定对象。您可以使用特殊键获取所需对象。所有可用键如下所示:

BHNV, BHNVS, BOR2, BOR3, BOR5, CAL2, CAL3, CAL5, CAL7, CENN, CENW, CLIN, CON1, CON2, CON3, CON4, DETA, DIAG, DILI, DIMP, DIMR, FEMN, FEMPN, FEMSP, FEMST, FEMVN, FEMVT, HATC, HIDN, HIDW, HLIN, HOCN, HOCW, INFO, LEAD, N_BHNV, N_CON1, N_CON2, N_CON3, N_CON4, N_CENN, N_CENW, N_DILI, N_HATC, N_HIDN, N_HIDW, N_LEAD, N_NGIP, N_PHAN, N_TCON, N_THLI, N_TLEN, N_VPLI, NGIP, PAL2, PAL3, PAL5, PHAN, POSN, RCGN, RCGW, BRK, SECT, SYB3, MSYM, TCON, THLI, TIT2, TIT3, TIT5, TLEN, TRAIL, TRLI, TXT1, TXT2, TXT3, TXT5, TXT7, VIEW, VEDH, VEDV, VPLI, ZIZA, SPLI。

// Note: The next block of code requires additional inclusion of "AmiLayer.h",
//       "AcmStandardManager.h" and "AmiLayObjCfg.h"

OdDbDatabase* pDb = ... // get a pointer to the current database

// get the current std ID
OdDbObjectId stdId;
if (getAcmStdMgr()->getCurrent(stdId, pDb) != Acm::eOk)
  return;

OdString objKey = ... // set a key of the mechanical layer object from the list above
AmiLayObjCfg* objProp = NULL;

AmiStatus status = amiGetLayerObjProperty(objKey, objProp, stdId);
if (status.success())
{
  OdCmColor color;
  color.setColorIndex(3);
  objProp->setTrueColor(color);
}
else
{
  ... // Failure during execution. Perhaps the input parameters are incorrect
}

// Don't forget to clear the memory because the ‘new’ operator was used for
// object creation

if (objProp)
  delete objProp;

今天就开始行动

免费试用 ODA 软件 60 天。
无风险,无需信用卡。

免费试用