|
| 1 | +""" |
| 2 | +Copyright 2019 Autodesk, Inc. All rights reserved. |
| 3 | +# https://forum.dynamobim.com/t/use-dynamo-to-create-and-manipulate-autocad-civil-3d-layers/38567/8 |
| 4 | +This file is part of the Civil 3D Python Module. |
| 5 | +
|
| 6 | +""" |
| 7 | +__author__ = 'Paolo Emilio Serra - paolo.serra@autodesk.com' |
| 8 | +__modifiedby__ = 'Deivid Steffens - deividsteffens@msn.com' |
| 9 | +__copyright__ = '2020' |
| 10 | +__version__ = '1.0.0' |
| 11 | + |
| 12 | +import clr |
| 13 | + |
| 14 | +# Add Assemblies for AutoCAD and Civil 3D APIs |
| 15 | +clr.AddReference('acmgd') |
| 16 | +clr.AddReference('acdbmgd') |
| 17 | +clr.AddReference('accoremgd') |
| 18 | +clr.AddReference('AecBaseMgd') |
| 19 | +clr.AddReference('AecPropDataMgd') |
| 20 | +clr.AddReference('AeccDbMgd') |
| 21 | +clr.AddReference('AeccPressurePipesMgd') |
| 22 | +clr.AddReference('acdbmgdbrep') |
| 23 | +clr.AddReference('System.Windows.Forms') |
| 24 | + |
| 25 | +# Add standard Python references |
| 26 | +import sys |
| 27 | +sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib') |
| 28 | +import os |
| 29 | +import math |
| 30 | + |
| 31 | +# Add references to manage arrays, collections and interact with the user |
| 32 | +from System import * |
| 33 | +from System.IO import * |
| 34 | +from System.Collections.Specialized import * |
| 35 | +from System.Windows.Forms import MessageBox |
| 36 | + |
| 37 | +# Create an alias to the Autodesk.AutoCAD.ApplicationServices.Application class |
| 38 | +import Autodesk.AutoCAD.ApplicationServices.Application as acapp |
| 39 | + |
| 40 | +# Import references from AutoCAD |
| 41 | +from Autodesk.AutoCAD.Runtime import * |
| 42 | +from Autodesk.AutoCAD.ApplicationServices import * |
| 43 | +from Autodesk.AutoCAD.EditorInput import * |
| 44 | +from Autodesk.AutoCAD.DatabaseServices import * |
| 45 | +from Autodesk.AutoCAD.Geometry import * |
| 46 | +from Autodesk.AutoCAD.Colors import Color |
| 47 | +from Autodesk.AutoCAD.Colors import ColorMethod |
| 48 | +# Import references for PropertySets |
| 49 | +from Autodesk.Aec.PropertyData import * |
| 50 | +from Autodesk.Aec.PropertyData.DatabaseServices import * |
| 51 | + |
| 52 | +# Import references for Civil 3D |
| 53 | +from Autodesk.Civil.ApplicationServices import * |
| 54 | +from Autodesk.Civil.DatabaseServices import * |
| 55 | + |
| 56 | +adoc = acapp.DocumentManager.MdiActiveDocument |
| 57 | +ed = adoc.Editor |
| 58 | + |
| 59 | +# Example function |
| 60 | +def create_layer(name, index): |
| 61 | + |
| 62 | + indx = int(index) |
| 63 | + |
| 64 | + # Fail gracefully |
| 65 | + if not isinstance(name, str) or len(name) == 0: |
| 66 | + raise Exception('The name is not a valid string') |
| 67 | + if not isinstance(indx, int) or indx < 0 or indx > 255: |
| 68 | + raise Exception('The index component is not a valid integer') |
| 69 | + |
| 70 | + # replace invalid characters in the name |
| 71 | + name = name.replace(':', '_').replace(';', '_').replace(',', '_') |
| 72 | + |
| 73 | + global adoc |
| 74 | + result = False |
| 75 | + |
| 76 | + with adoc.LockDocument(): |
| 77 | + with adoc.Database as db: |
| 78 | + with db.TransactionManager.StartTransaction() as t: |
| 79 | + # Get the Layer Table Object |
| 80 | + lt = t.GetObject(db.LayerTableId, OpenMode.ForRead) |
| 81 | + # Check if the layer already exists |
| 82 | + if not lt.Has(name): |
| 83 | + with LayerTableRecord() as ltr: |
| 84 | + ltr = LayerTableRecord() |
| 85 | + ltr.Name = name |
| 86 | + ltr.Color = Color.FromColorIndex(ColorMethod.ByAci,indx) |
| 87 | + lt.UpgradeOpen() |
| 88 | + lt.Add(ltr) |
| 89 | + lt.DowngradeOpen() |
| 90 | + t.AddNewlyCreatedDBObject(ltr, True) |
| 91 | + else: |
| 92 | + ltr = t.GetObject(lt[name], OpenMode.ForWrite) |
| 93 | + # Set the color |
| 94 | + if ltr is not None: |
| 95 | + ltr.Color = Color.FromColorIndex(ColorMethod.ByAci,indx) |
| 96 | + |
| 97 | + result = True |
| 98 | + t.Commit() |
| 99 | + |
| 100 | + return result |
| 101 | + |
| 102 | +for i in range(10): |
| 103 | + name = "Test"+ str(i) |
| 104 | + create_layer(name,i) |
| 105 | + print("Created Layer",name) |
| 106 | + |
0 commit comments