[返回博客](/cn/blog) # 使用机械图层和图层组（第 1 部分，共 2 部分）

Dmytro Kabenok 五月 27, 2021 [\#mechanical](/cn/blog/tag/mechanical) [\#getting started](/cn/blog/tag/getting-started)

<a class="a2a_button_facebook"></a><a class="a2a_button_twitter"></a><a class="a2a_button_linkedin"></a>

#### 概述

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 天。  
无风险，无需信用卡。**

[免费试用](https://www.opendesign.com/cn/free-trial)
