📜  获取文本内容 dynamo Civil 3d - Python 代码示例

📅  最后修改于: 2022-03-11 14:46:35.858000             🧑  作者: Mango

代码示例1
import sys
import clr

# Add Assemblies for AutoCAD and Civil3D
clr.AddReference('AcMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AecBaseMgd')
clr.AddReference('AecPropDataMgd')
clr.AddReference('AeccDbMgd')

# Import references from AutoCAD
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *

# Import references from Civil3D
from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *

adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor

 # Add DesignScript references
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

# Enable Python support and load DesignScript library
from System.Collections.Generic import Dictionary

def get_text_props():
    
    textProps = []
    obj=[]
    keys = ["Contents","Horizontal mode","Vertical mode","Height","Rotation","Width factor","Obliquing","Alignment point","Insertion point","Upside down","Backward"]
    errorReport = None
    
    handles = []
    global adoc
    
    with adoc.LockDocument():
        with adoc.Database as db:
            with db.TransactionManager.StartTransaction() as t:
                bt = t.GetObject(db.BlockTableId, OpenMode.ForWrite)
                btr = t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite)
                for oid in btr:
                    bl = t.GetObject(oid, OpenMode.ForRead)
                    if isinstance(bl, DBText):
                        handles.append(str(bl.Handle))
                for h in handles:
                    oid=(db.GetObjectId(False,Handle(int(h,16)),0))
                    obj.append(t.GetObject(oid, OpenMode.ForRead))                    
                # Create list of properties for each DBtext
                for i in obj:
                    val=[]
                    try:
                        
                        val.append(i.TextString)
                        
                        #Remove "Text" from beginning of text alignment modes
                        hMode=str(i.HorizontalMode)
                        vMode=str(i.VerticalMode)
                        hMode_trim=hMode.Replace("Text","")
                        vMode_trim=vMode.Replace("Text","")
                        val.append(hMode_trim)
                        val.append(vMode_trim)
                        
                        val.append(i.Height)
                        val.append(i.Rotation)
                        val.append(i.WidthFactor)
                        val.append(i.Oblique)
                        
                        # Create alignment point
                        alignX=i.AlignmentPoint.X
                        alignY=i.AlignmentPoint.Y
                        alignZ=i.AlignmentPoint.Z
                        alignPt=Point.ByCoordinates(alignX,alignY,alignZ)
                        val.append(alignPt)
                        
                        #Create insertion point
                        insX=i.Position.X
                        insY=i.Position.Y
                        insZ=i.Position.Z
                        insPt=Point.ByCoordinates(insX,insY,insZ)
                        val.append(insPt)
                        
                        val.append(i.IsMirroredInY)
                        val.append(i.IsMirroredInX)                
                    # Error handling
                    except:
                        import traceback
                        errorReport = traceback.format_exc()    
                    d = {k:v for k,v in zip(keys,val)}
                    dict = Dictionary[str,object](d)
                    textProps.append(dict)        
                
                t.Commit()            
    if errorReport == None:
        return textProps
    else:
        return errorReport
    
OUT = get_text_props()