[ブログに戻る](/ja/blog) # メカニカルレイヤーとレイヤーグループの操作 (1/2)

Dmytro Kabenok 5月 27, 2021 [\#mechanical](/ja/blog/tag/mechanical) [\#getting started](/ja/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 は定義済みのレイヤー上にのみオブジェクトを作成します。レイヤーが存在しない場合は、自動的に作成されます。これらのメカニカルレイヤーには、レイヤーの色、線種など、事前定義された設定があり、これらはレイヤー定義と呼ばれる事前設定された設定セットから取得されます。

複数のメカニカルレイヤーセットを作成できます。各セットはレイヤーグループと呼ばれます。メカニカルレイヤーグループを使用すると、次のことができます。

- オブジェクトをあるレイヤーグループから別のレイヤーグループに移動またはコピーする。
- レイヤーグループの一部である個々のレイヤーをグローバルに操作する。たとえば、作成したレイヤーグループに関係なく、線種を変更できます。
- レイヤーグループの色を設定し、個々のレイヤーの色をレイヤーグループの色で上書きする。
- 設計作業を簡素化するためにレイヤーグループをロックおよびフリーズする。

必要なクラスと関数が宣言されているファイルは3つあります。

- 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];
```

1つのメカニカルレイヤーの値のみを変更する場合、配列全体を取得してその中の特定のオブジェクトを探す必要はありません。以下のコード例を使用して変更できます。

```
// 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];
```

1つのメカニカルレイヤーオブジェクトの値のみを変更する場合（メカニカルレイヤーの例と同様に）、配列全体を取得してその中の特定のオブジェクトを探す必要はありません。特別なキーを使用して目的のオブジェクトを取得できます。利用可能なすべてのキーは以下のとおりです。

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/ja/free-trial)
