03-06 创建和编辑AutoCAD实体(六) 保存和恢复图层状态

Save and Restore Layer States保存和恢复图层状态

You can save layer states in a drawing andrestore them later. This makes it easy to return to specified settings for alllayers during different stages when completing a drawing or when plotting adrawing.

我们可以在图形中保存图层状态,并在随后将其恢复。这使得我们能够很容易在图形绘制或打印图形的不同步骤之间返回到图层的指定设置状态。

Layer states include whether or not alayer is turned on, frozen, locked, plotted, and automatically frozen in newviewports, and the layer's color, linetype, lineweight, and plot style. You canspecify which settings you want to save, and you can save different groups ofsettings for a drawing.

图层状态包括图层是否打开、冻结、锁定、可打印,及在新视口是否自动冻结,以及图层的颜色、线型、线宽、打印样式等(信息)。我们可以指定要保存哪些设置,并且可以给一个图形保存几组不同的设置。

The LayerStateManager is used to save andrestore layer states.

LayerStateManager用来保存和恢复图层状态。

For more information about saving layersettings, see “Save and Restore Layer Settings” in the AutoCAD User's Guide.

更多关于保存图层设置的内容,见AutoCAD用户指南中的“保存和恢复图层设置”。

Topics in this section本节主题:

·      Understand How AutoCAD Saves Layer States了解AutoCAD如何保存图层状态

·      Use the LayerStateManager to Manage Layer States使用LayerStateManager管理图层状态

 

1、了解AutoCAD如何保存图层状态

AutoCAD saves layer setting information inan extension dictionary of the Layers table object. When you first save a layerstate, AutoCAD does the following:

AutoCAD将图层设置信息保存在Layers表对象的扩展字典里。我们第一次保存图层状态时,AutoCAD会进行下列操作:

·        Creates an extension dictionary on the Layers table.

·        Layers表上创建一个扩展字典;

·        Creates a Dictionary object named ACAD_LAYERSTATE in theextension dictionary.

·        在扩展字典里创建一个名为ACAD_LAYERSTATEDictionary对象;

·        Stores the properties of each layer in the drawing in anXRecord object in the ACAD_LAYERSTATE dictionary. AutoCAD stores all layersettings in the XRecord, but identifies the specific settings you chose tosave. When you restore the layer settings, AutoCAD restores only the settingsyou chose to save.

·        将图形中每个图层的属性存储在ACAD_LAYERSTATE字典的XRecord对象中。AutoCAD将所有的图层设置存储在XRecord里,但可以识别出你选择保存的那些设置。当恢复图层设置时,AutoCAD只恢复你选择保存的那些设置。

Each time you save another layer settingin the drawing, AutoCAD creates another XRecord object describing the savedsettings and stores the XRecord in the ACAD_LAYERSTATE dictionary. Thefollowing diagram illustrates the process.

每当我们在图形中保存另一个图层设置时,AutoCAD都会创建一个新的XRecord对象来描述所保存的设置,并将该XRecord对象存储到ACAD_LAYERSTATE字典里。下图为这一过程的图解。



You do not need (and should not try) todirectly manipulate the entries when working with layer states. Use thefunctions of the LayerStateManager object to access the dictionary. Once youhave a reference to the dictionary, you can step through each of the entries whichare represented as DBDictionaryEntry objects.

在使用图层状态时,我们不需要(也不要试图)去直接操作那些实体,使用LayerStateManager对象的方法来访问字典就可以了。我们一旦获得了对字典的引用,就可以遍历代表DBDictionaryEntry对象的每一个实体。

List the saved layer states in a drawing列出图形中保存的图层状态

If layer states have been saved in thecurrent drawing, the following code lists the names of all saved layer states:

如果图层状态已经保存在当前图形内,下面的代码会列出保存的所有图层状态的名称:

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.DatabaseServices

 

<CommandMethod("ListLayerStates")>_

Public SubListLayerStates()

  '' Get the current document and database获取当前文档及数据库

  Dim acDoc As Document =Application.DocumentManager.MdiActiveDocument

  Dim acCurDb As Database = acDoc.Database

 

  '' Start a transaction启动事务

  Using acTrans As Transaction =acCurDb.TransactionManager.StartTransaction()

 

      Dim acLyrStMan As LayerStateManager

      acLyrStMan = acCurDb.LayerStateManager

 

      Dim acDbDict As DBDictionary

      acDbDict =acTrans.GetObject(acLyrStMan.LayerStatesDictionaryId(True), _

                                  OpenMode.ForRead)

 

      Dim sLayerStateNames As String =""

 

      For Each acDbDictEnt As DBDictionaryEntryIn acDbDict

          sLayerStateNames = sLayerStateNames& vbLf & acDbDictEnt.Key

      Next

 

      Application.ShowAlertDialog("Thesaved layer settings in this drawing are:" & _

                                 sLayerStateNames)

 

      '' Dispose of the transaction

  End Using

End Sub

C#

usingAutodesk.AutoCAD.Runtime;

usingAutodesk.AutoCAD.ApplicationServices;

usingAutodesk.AutoCAD.DatabaseServices;

 

[CommandMethod("ListLayerStates")]

public static voidListLayerStates()

{

  // Get the current document and database获取当前文档及数据库

  Document acDoc =Application.DocumentManager.MdiActiveDocument;

  Database acCurDb = acDoc.Database;

 

  // Start a transaction启动事务

  using (Transaction acTrans =acCurDb.TransactionManager.StartTransaction())

  {

      LayerStateManager acLyrStMan;

      acLyrStMan = acCurDb.LayerStateManager;

 

      DBDictionary acDbDict;

      acDbDict =acTrans.GetObject(acLyrStMan.LayerStatesDictionaryId(true),

                                  OpenMode.ForRead) as DBDictionary;

 

      string sLayerStateNames = "";

 

      //遍历

      foreach (DBDictionaryEntry acDbDictEnt inacDbDict)

      {

          sLayerStateNames = sLayerStateNames +"\n" + acDbDictEnt.Key;

      }

 

      Application.ShowAlertDialog("Thesaved layer settings in this drawing are:" +

                                  sLayerStateNames);

 

      // Dispose of the transaction撤销事务

  }

}

VBA/ActiveX CodeReference

Sub ListLayerStates()

     On Error Resume Next

    Dim oLSMDict As AcadDictionary

    Dim XRec As Object

    Dim layerstateNames As String

 

    layerstateNames = ""

    ' Get the ACAD_LAYERSTATES dictionary,which is in the

    ' extension dictionary in the Layersobject.

    Set oLSMDict = ThisDrawing.Layers. _

                              GetExtensionDictionary.Item("ACAD_LAYERSTATES")

 

    'List the name of each saved layer setting. Settings are

    ' stored as XRecords in the dictionary.

    For Each XRec In oLSMDict

       layerstateNames = layerstateNames +XRec.Name + vbCrLf

    Next XRec

 

    MsgBox "The saved layer settings inthis drawing are: " + _

           vbCrLf + layerstateNames

End Sub

 

2、用LayerStateManager管理图层状态

The LayerStateManager object provides aset of functions for creating and manipulating saved layer states. Use thefollowing LayerStateManager functions for working with layer states:

LayerStateManager对象提供了一组创建和操作保存的图层状态的方法。这些方法如下:

DeleteLayerState

Deletes a saved layer state. 删除一个保存的图层状态;

ExportLayerState

Exports the specified saved layer state to a LAS file. 将制定的图层状态导出为LAS文件;

ImportLayerState

Imports a layer state from the specified LAS file. LAS文件导入图层状态;

ImportLayerStateFromDb

Imports a layer state from anotherdatabase. 从别的数据库导入图层状态;

RenameLayerState

Renames a saved layer state. 重命名保存的图层状态;

RestoreLayerState

Restores the specified layer state in the currentdrawing. 恢复当前图形中制定的图层状态;

SaveLayerState

Saves the specified layer state and its properties. 保存制定的图层状态及其属性;

 

The LayerStateManager object for adatabase can be accessed by using the LayerStateManager property of a Database object.

数据库的LayerStateManager对象可以通过使用Database对象的LayerStateManager属性来访问。

VB.NET

Dim acDoc As Document= Application.DocumentManager.MdiActiveDocument

Dim acCurDb AsDatabase = acDoc.Database

 

Dim acLyrStMan AsLayerStateManager

acLyrStMan =acCurDb.LayerStateManager

C#

Document acDoc =Application.DocumentManager.MdiActiveDocument;

Database acCurDb =acDoc.Database;

 

LayerStateManageracLyrStMan;

acLyrStMan =acCurDb.LayerStateManager;

 

VBA/ActiveX Code ReferenceVBA/ActiveX代码参考

After you retrieve the LayerStateManagerobject, you must associate a database with it before you can access theobject's methods. Use the SetDatabase method to associate a database with theLayerStateManager.

获取LayerStateManager对象之后,在访问对象的方法之前必须将一个数据库与之关联。将数据库与LayerStateManager对象关联用SetDatabase方法。

Dim oLSM AsAcadLayerStateManager

Set oLSM =ThisDrawing.Application. _

              GetInterfaceObject("AutoCAD.AcadLayerStateManager.18")

 

oLSM.SetDatabaseThisDrawing.Database

 

Topics in this section本小节主题:

 

·        Save Layer States保存图层状态

·        Rename Layer States重命名图层状态

·        Delete Layer States删除图层状态

·        Restore Layer States恢复图层状态

·        Export and Import Saved Layer States导入导出保存的图层状态

 

2.1、SaveLayer States保存图层状态

Use the SaveLayerState method to save a set of layer settings ina drawing. The SaveLayerState method requires three parameters. The first parameter isa string naming the layer state you are saving. The second parameter identifiesthe layer properties you want to save. Use the constants of the LayerStateMasks enum to identify the layer settings youwant to save. The following table lists the constants that are part of the LayerStateMasks enum.

使用SaveLayerState方法保存图形中的一组图层设置。SaveLayerState方法需要3个参数:

第一个参数为一个字符串,用于命名要保存的图层状态;

第二个参数表示要保存的图层属性。使用枚举LayerStateMasks定义的常量来识别要保存的图层设置。下表列出了枚举LayerStateMasks定义的部分常量:

 

Constants for layer state mask图层状态掩码常量

Constant name常量名称

Layer property图层属性

Color

Color

CurrentViewport

Current viewport layers frozen or thawed

Frozen

Frozen or thawed

LastRestored

Last restored layer

LineType

Linetype

LineWeight

Lineweight

Locked

Locked or unlocked

NewViewport

New viewport layers frozen or thawed

None

No layer settings

On

On or off

Plot

Plotting on or off

PlotStyle

Plot style

 

Add the constants together to specifymultiple properties.

指定多个属性只需将这些常量加在一起即可。

The third parameter required is the objectid of the viewport whose layer settings you want to save. Use ObjectId.Null tonot specify a viewport. If you try to save a layer state under a name thatalready exists, an error is returned. You must rename or delete the existinglayer state before you can reuse the name.

第三个参数为所要保存的图层设置所在的视口的对象标示(ObjectId)。使用ObjectId.Null表示不指定视口。如果试图使用已存在的名字来保存图层状态就会返回错误。在重用名字前必须将以存在的图层状态改名或干脆删除掉。

Save a layer's color and linetypesettings 保存图层的颜色和线型设置

The following code saves the color andlinetype settings of the current layers in the drawing under the nameColorLinetype.

下列代码将图形中当前图层的颜色和线型设置保存到名为ColorLinetype的图层状态中。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

 

<CommandMethod("SaveLayerColorAndLinetype")>_

Public SubSaveLayerColorAndLinetype()

  '' Get the current document

  Dim acDoc As Document =Application.DocumentManager.MdiActiveDocument

 

  Dim acLyrStMan As LayerStateManager

  acLyrStMan = acDoc.Database.LayerStateManager

 

  Dim sLyrStName As String ="ColorLinetype"

 

  If acLyrStMan.HasLayerState(sLyrStName) =False Then

      acLyrStMan.SaveLayerState(sLyrStName, _

                               LayerStateMasks.Color + _

                               LayerStateMasks.LineType, _

                                ObjectId.Null)

  End If

End Sub

C#

usingAutodesk.AutoCAD.Runtime;

usingAutodesk.AutoCAD.ApplicationServices;

usingAutodesk.AutoCAD.DatabaseServices;

 

[CommandMethod("SaveLayerColorAndLinetype")]

public static voidSaveLayerColorAndLinetype()

{

  // Get the current document获取当前文档

  Document acDoc =Application.DocumentManager.MdiActiveDocument;

 

  LayerStateManager acLyrStMan;

  acLyrStMan =acDoc.Database.LayerStateManager;

 

  string sLyrStName ="ColorLinetype";

 

  if (acLyrStMan.HasLayerState(sLyrStName) ==false)

  {

      acLyrStMan.SaveLayerState(sLyrStName,

                               LayerStateMasks.Color |

                               LayerStateMasks.LineType,

                                ObjectId.Null);

  }

}

VBA/ActiveX Code Reference

SubSaveLayerColorAndLinetype()

    Dim oLSM As AcadLayerStateManager

 

    ' Access the LayerStateManager object

    Set oLSM = ThisDrawing.Application. _

                  GetInterfaceObject("AutoCAD.AcadLayerStateManager.18")

 

    ' Associate the current drawing databasewith LayerStateManager

    oLSM.SetDatabase ThisDrawing.Database

    oLSM.Save "ColorLinetype",acLsColor + acLsLineType

End Sub

 

2.2、RenameLayer States重命名图层状态

The RenameLayerState method renames a saved layer state fromone name to another in a drawing. The following code renames the ColorLinetypelayer settings to OldColorLinetype.

RenameLayerState方法用来修改图形中已保存的图层状态的名称。下列代码将图层设置ColorLinetype改名为OldColorLinetype

VB.NET

Imports Autodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

 

<CommandMethod("RenameLayerState")>_

Public SubRenameLayerState()

  '' Get the current document

  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument

 

  Dim acLyrStMan As LayerStateManager

  acLyrStMan = acDoc.Database.LayerStateManager

 

  Dim sLyrStName As String ="ColorLinetype"

  Dim sLyrStNewName As String ="OldColorLinetype"

 

  If acLyrStMan.HasLayerState(sLyrStName) =True And _

     acLyrStMan.HasLayerState(sLyrStNewName) =False Then

      acLyrStMan.RenameLayerState(sLyrStName,sLyrStNewName)

  End If

End Sub

C#

usingAutodesk.AutoCAD.Runtime;

usingAutodesk.AutoCAD.ApplicationServices;

usingAutodesk.AutoCAD.DatabaseServices;

 

[CommandMethod("RenameLayerState")]

public static voidRenameLayerState()

{

  // Get the current document

  Document acDoc =Application.DocumentManager.MdiActiveDocument;

 

  LayerStateManager acLyrStMan;

  acLyrStMan = acDoc.Database.LayerStateManager;

 

  string sLyrStName ="ColorLinetype";

  string sLyrStNewName ="OldColorLinetype";

 

  if (acLyrStMan.HasLayerState(sLyrStName) ==true &&

      acLyrStMan.HasLayerState(sLyrStNewName)== false)

  {

      acLyrStMan.RenameLayerState(sLyrStName,sLyrStNewName);

  }

}

VBA/ActiveX Code Reference

SubRenameLayerState()

    Dim oLSM As AcadLayerStateManager

    Set oLSM = ThisDrawing.Application. _

                  GetInterfaceObject("AutoCAD.AcadLayerStateManager.18")

 

    oLSM.SetDatabase ThisDrawing.Database

    oLSM.Rename "ColorLinetype","OldColorLinetype"

End Sub

 

2.3、DeleteLayer States删除图层状态

The DeleteLayerState method removes a saved layer state from adrawing. The following code deletes the layer state saved under the name ColorLinetype.

DeleteLayerState方法从图形中移除保存的图层状态。下列代码删除保存的名为ColorLinetype的图层状态。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

 

<CommandMethod("RemoveLayerState")>_

Public SubRemoveLayerState()

  '' Get the current document

  Dim acDoc As Document =Application.DocumentManager.MdiActiveDocument

 

  Dim acLyrStMan As LayerStateManager

  acLyrStMan = acDoc.Database.LayerStateManager

 

  Dim sLyrStName As String = "ColorLinetype"

 

  If acLyrStMan.HasLayerState(sLyrStName) =True Then

      acLyrStMan.DeleteLayerState(sLyrStName)

  End If

End Sub

C#

usingAutodesk.AutoCAD.Runtime;

usingAutodesk.AutoCAD.ApplicationServices;

usingAutodesk.AutoCAD.DatabaseServices;

 

[CommandMethod("RemoveLayerState")]

public static voidRemoveLayerState()

{

  // Get the current document

  Document acDoc =Application.DocumentManager.MdiActiveDocument;

 

  LayerStateManager acLyrStMan;

  acLyrStMan =acDoc.Database.LayerStateManager;

 

  string sLyrStName ="ColorLinetype";

 

  if (acLyrStMan.HasLayerState(sLyrStName) ==true)

  {

      acLyrStMan.DeleteLayerState(sLyrStName);

  }

}

VBA/ActiveX Code Reference

SubRemoveLayerState()

    Dim oLSM As AcadLayerStateManager

    Set oLSM = ThisDrawing.Application. _

                  GetInterfaceObject("AutoCAD.AcadLayerStateManager.18")

 

    oLSM.SetDatabase ThisDrawing.Database

    oLSM.Delete "ColorLinetype"

End Sub

 

2.4、RestoreLayer States恢复图层状态

The RestoreLayerState method resets the layer settings in alayer state and requires four values. The first value is the name of the layerstate to restore, and the second value is the object id of the viewport whoselayer settings you want to restore. The third value is an integer that defineshow layers not in the layer state are handled. The fourth value determineswhich layer settings are restored.

RestoreLayerState方法用来恢复图层状态中的图层设置,该方法需要4个参数:

第一个参数要恢复的图层状态的名称;

第二个参数要恢复的图层状态所在的视口的ObjectId

第三个参数是个整数,定义如何处理图层状态中没有的图层;

第四个参数定义恢复哪些图层属性设置;

The following values determine how layersnot in a layer state are handled:

下列值确定如何处理图层状态中没有的图层:

·        0 - Layers not in the layer state are left unchanged 图层状态中没有的图层保持不变;

·        1 - Layers not in the layer state are turned Off 关闭图层状态中没有的图层;

·        2 - Layers not in the layer state are frozen in thecurrent viewport 在当前视口中冻结图层状态中没有的图层;

·        4 - Layer settings are restored as viewport overrides 以覆盖视口的方式恢复图层设置;

Note You can use the sum of multiple values previous listed todefine the restore behavior of layers not in a layer state. For example, youcan turn off and freeze the layers that are not saved with a layer state.

注意:可以使用上述的多个值得合计来定义不在图层状态中的图层的恢复行为。例如,可以关闭并冻结图层状态中没有的图层。

For example, if you save the color andlinetype settings under the name “ColorLinetype” and subsequently change thosesettings, restoring “ColorLinetype” resets the layers to the colors andlinetypes they had when “ColorLinetype” was saved. If you add new layers to thedrawing after saving “ColorLinetype,” those new layers are not affected whenyou restore “ColorLinetype.”

例如,如果将颜色和线型设置保存到名为“ColorLinetype”的图层状态下,随后修改了这些设置,那么,恢复“ColorLinetype”就会将图层的颜色和线型重新设置为保存“ColorLinetype”时的状态。如果保存“ColorLinetype”后在图形中添加了新图层,当恢复“ColorLinetype”时新图层将不受影响。

Restore the color and linetype settingsof a drawing's layers 恢复图形中图层的颜色和线型设置

Assuming that the color and linetypesettings of the layers in the current drawing were previously saved under thename “ColorLinetype,” the following code restores the color and linetypesettings of each layer in the drawing to the value they had when“ColorLinetype” was saved.

假设前面已经将当前图形中的图层的颜色和线型设置保存到“ColorLinetype”名下,下列代码将会将图形中每个图层的颜色和线型设置恢复到保存“ColorLinetype”时的状态。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

 

<CommandMethod("RestoreLayerState")>_

Public SubRestoreLayerState()

  '' Get the current document

  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument

 

  Dim acLyrStMan As LayerStateManager

  acLyrStMan = acDoc.Database.LayerStateManager

 

  Dim sLyrStName As String ="ColorLinetype"

 

  If acLyrStMan.HasLayerState(sLyrStName) =True Then

      acLyrStMan.RestoreLayerState(sLyrStName,_

                                  ObjectId.Null, _

                                   1, _

                                  LayerStateMasks.Color + _

                                  LayerStateMasks.LineType)

  End If

End Sub

C#

usingAutodesk.AutoCAD.Runtime;

usingAutodesk.AutoCAD.ApplicationServices;

usingAutodesk.AutoCAD.DatabaseServices;

 

[CommandMethod("RestoreLayerState")]

public static voidRestoreLayerState()

{

  // Get the current document

  Document acDoc =Application.DocumentManager.MdiActiveDocument;

 

  LayerStateManager acLyrStMan;

  acLyrStMan =acDoc.Database.LayerStateManager;

 

  string sLyrStName ="ColorLinetype";

 

  if (acLyrStMan.HasLayerState(sLyrStName) ==true)

  {

      acLyrStMan.RestoreLayerState(sLyrStName,

                                  ObjectId.Null,

                                   1,

                                  LayerStateMasks.Color |

                                  LayerStateMasks.LineType);

  }

}

VBA/ActiveX Code Reference

SubRestoreLayerState()

    Dim oLSM As AcadLayerStateManager

    Set oLSM = ThisDrawing.Application. _

                  GetInterfaceObject("AutoCAD.AcadLayerStateManager.18")

 

    oLSM.SetDatabase ThisDrawing.Database

    oLSM.Restore "ColorLinetype"

End Sub

 

2.5、Exportand Import Saved Layer States导出导入保存的图层状态

You can export and import saved layerstates to use the same layer settings in other drawings. Use the ExportLayerState method to export a saved layer state toan LAS file; use the ImportLayerState method to import a LAS file into adrawing.

我们可以导出导入保存的图层状态,以便在别的图形文件中使用相同的图层设置。将保存的图层状态导出到LAS文件用ExportLayerState方法;将LAS文件导入到图形用ImportLayerState方法。

Note Importing layer states does not restore them; you mustuse the RestoreLayerState method to restore the layer state afterit is imported.

注意:导入图层状态并不恢复这些图层;必须在导入后使用RestoreLayerState方法来恢复图层状态。

The ExportLayerState method accepts two parameters. The firstparameter is a string identifying the saved layer state to export. The secondparameter is the name of the file you are exporting the layer state to. If youdo not specify a path for the file, it is saved in the same directory in whichthe drawing was opened from. If the file name you specified already exists, theexisting file is overwritten. Use a .las extension when naming files;this is the extension AutoCAD recognizes for exported layer state files.

ExportLayerState方法接收两个参数。第一个参数是个字符串,代表要导出的图层状态。第二个参数是保存所导出图层状态的文件名。如不指定文件路径,文件将保存到与打开的图形文件相同的目录中。如指定的文件名已经存在,将被覆盖。命名文件时使用.las扩展名,AutoCAD识别该扩展名为导出的图层状态文件。

The ImportLayerState method accepts one parameter: a stringnaming the file that contains the layer states you are importing. If the layerstate you want to import does not exist in a LAS file, but a drawing file. Youcan open the drawing file and then use the ImportLayerStateFromDb method to import a layer state from theDatabase object of the other drawing.

ImportLayerState方法有一个参数:一个字符串,代表包含要导入的图层状态的文件名。如果要导入的图层状态不在LAS文件里,而是在图形文件里,我们可以先打开图形文件,然后使用ImportLayerStateFromDb方法从该图形的Database对象中导入图层状态。

When you import layer states, an errorcondition is raised if any properties referenced in the saved settings are notavailable in the drawing you are importing to. The import is completed,however, and default properties are used. For example, if an exported layer isset to a linetype that is not loaded in the drawing it is being imported into,an error condition is raised and the drawing's default linetype is substituted.Your code should account for this error condition and continue processing if itis raised.

当导入图层状态时,如果保存的设置里引用的某个属性在要导入的图形中不可用,就会出现错误情况,不过导入操作会使用默认属性继续进行至结束。例如,如果导出的图层设置的线型在要导入的图形中没有被加载,就会出现错误情况,并且线型会由图形的默认线型代替。我们编程应化解这个错误情况,并在出错的情况下能继续运行。

If the imported file defines settings forlayers that do not exist in the current drawing, those layers are created inthe current drawing. When you use the RestoreLayerState method, the properties specified when thesettings were saved are assigned to the new layers; all other properties of thenew layers are assigned default settings.

如果导入文件中定义的图层设置在当前图形中不存在,就会在当前图形中创建这些图层。当使用RestoreLayerState方法时,保存图层状态时指定的属性会赋给新图层;而新图层的所有其他属性则使用默认设置。

Export saved layer settings 导出保存的图层设置

The following example exports a savedlayer state to a file named ColorLinetype.las.

下例将保存的图层状态导出到文件ColorLinetype.las

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

 

<CommandMethod("ExportLayerState")>_

Public SubExportLayerState()

  '' Get the current document

  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument

 

  Dim acLyrStMan As LayerStateManager

  acLyrStMan = acDoc.Database.LayerStateManager

 

  Dim sLyrStName As String ="ColorLinetype"

 

  If acLyrStMan.HasLayerState(sLyrStName) =True Then

      acLyrStMan.ExportLayerState(sLyrStName,"c:\my documents\" & _

                                             sLyrStName & ".las")

  End If

End Sub

C#

usingAutodesk.AutoCAD.Runtime;

usingAutodesk.AutoCAD.ApplicationServices;

usingAutodesk.AutoCAD.DatabaseServices;

 

[CommandMethod("ExportLayerState")]

public static voidExportLayerState()

{

  // Get the current document获取当前文档

  Document acDoc =Application.DocumentManager.MdiActiveDocument;

  LayerStateManager acLyrStMan;

  acLyrStMan = acDoc.Database.LayerStateManager;

 

  string sLyrStName ="ColorLinetype";

 

  if (acLyrStMan.HasLayerState(sLyrStName) ==true)

  {

      acLyrStMan.ExportLayerState(sLyrStName,"c:\\my documents\\" +

                                             sLyrStName + ".las");

  }

}

VBA/ActiveX Code Reference

SubExportLayerStates()

    Dim oLSM As AcadLayerStateManager

    Set oLSM = ThisDrawing.Application. _

      GetInterfaceObject("AutoCAD.AcadLayerStateManager.18")

 

    oLSM.SetDatabase ThisDrawing.Database

    oLSM.Export "ColorLinetype","c:\my documents\ColorLinetype.las"

End Sub

Import saved layer settings 导入保存的图层设置

The following example imports the layerstate from a file named ColorLinetype.las.

下例从文件ColorLinetype.las导入图层状态。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

 

<CommandMethod("ImportLayerState")>_

Public SubImportLayerState()

  '' Get the current document

  Dim acDoc As Document =Application.DocumentManager.MdiActiveDocument

 

  Dim acLyrStMan As LayerStateManager

  acLyrStMan = acDoc.Database.LayerStateManager

 

  Dim sLyrStFileName As String = "c:\mydocuments\ColorLinetype.las"

 

  If System.IO.File.Exists(sLyrStFileName) Then

      Try

          acLyrStMan.ImportLayerState(sLyrStFileName)

      Catch ex AsAutodesk.AutoCAD.Runtime.Exception

         Application.ShowAlertDialog(ex.Message)

      End Try

  End If

End Sub

C#

usingAutodesk.AutoCAD.Runtime;

usingAutodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

 

[CommandMethod("ImportLayerState")]

public static voidImportLayerState()

{

  // Get the current document获取当前文档

  Document acDoc =Application.DocumentManager.MdiActiveDocument;

 

  LayerStateManager acLyrStMan;

  acLyrStMan =acDoc.Database.LayerStateManager;

 

  string sLyrStFileName = "c:\\mydocuments\\ColorLinetype.las";

 

  if (System.IO.File.Exists(sLyrStFileName))

  {

      try

      {

         acLyrStMan.ImportLayerState(sLyrStFileName);

      }

      catch (Autodesk.AutoCAD.Runtime.Exceptionex)

      {

         Application.ShowAlertDialog(ex.Message);

      }

  }

}

VBA/ActiveX Code Reference

SubImportLayerStates()

    Dim oLSM As AcadLayerStateManager

    Set oLSM = ThisDrawing.Application. _

                  GetInterfaceObject("AutoCAD.AcadLayerStateManager.18")

    oLSM.SetDatabase ThisDrawing.Database

 

    ' If the drawing you're importing to doesnot contain

    ' all the linetypes referenced in the savedsettings,

    ' an error is returned. The import iscompleted, though,

    ' and the default linetype is used.

    On Error Resume Next

    oLSM.Import "c:\mydocuments\ColorLType.las"

 

    If Err.Number = -2145386359 Then

       ' Error indicates a linetype is notdefined

       MsgBox ("One or more linetypesspecified in the imported " + _

               "settings is not defined inyour drawing")

    End If

 

    On Error GoTo 0

End Sub

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值