Message 1 of 4

Not applicable
07-07-2016
04:00 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
The block is simple with 3 attributes, no fields, no preset, no prompts. Goal - Insert block, pull area fields from existing closed polylines, insert fields in attributes of blocks.
Right now it shows a partial of the field formula in the block when inserted. Need it to show the value of the field.
Imports System.IO Imports Autodesk.AutoCAD.ApplicationServices Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.EditorInput Imports Autodesk.AutoCAD.Geometry Imports Autodesk.AutoCAD.Runtime Public Class BaseArea Property AcDoc As Document = Core.Application.DocumentManager.MdiActiveDocument Property AcDb As Database = AcDoc.Database Property AcEd As Editor = AcDoc.Editor Property Origlayer As String = Core.Application.GetSystemVariable("clayer") Property Origosmode = Core.Application.GetSystemVariable("osmode") Property Origorthomode = Core.Application.GetSystemVariable("orthomode") Property OrigSnapAngle = Core.Application.GetSystemVariable("snapang") Property OrigDimScale = Core.Application.GetSystemVariable("dimscale") Property OrigDimStyle = Core.Application.GetSystemVariable("DIMSTYLE") Property LayList As New List(Of String) Property GStr As String Property AStr As String Property NStr As String Property BlkTab As BlockTable Property BlkTabRec As BlockTableRecord Property BlkRef As BlockReference Property BlkRefId As ObjectId Property Fstr As String <CommandMethod("xxx")> Public Sub areafields() LayList.Add("AREA") LayList.Add("AREA-AC") LayList.Add("AREA-NonAC") For Each item In LayList Dim ents As ObjectIdCollection = GetEntitiesOnLayer(item.ToString) Using AcTrans As Transaction = AcDb.TransactionManager.StartTransaction Dim AcBlkTab As BlockTable = AcTrans.GetObject(AcDb.BlockTableId, OpenMode.ForRead) For Each entid As ObjectId In ents Dim pl As Polyline = DirectCast(AcTrans.GetObject(entid, OpenMode.ForRead), Polyline) If pl.Layer = "AREA" Then GStr = String.Format("%<\AcObjProp Object(%<\_ObjId " & pl.ObjectId.ToString & " \f ""%lu2%ct4%qf1 SQ. FT."">%", GStr) ElseIf pl.Layer = "AREA-AC" Then AStr = String.Format("%<\AcObjProp Object(%<\_ObjId " & pl.ObjectId.ToString & " \f ""%lu2%ct4%qf1 SQ. FT."">%", AStr) ElseIf pl.Layer = "AREA-nonAC" Then NStr = String.Format("%<\AcObjProp Object(%<\_ObjId " & pl.ObjectId.ToString & " \f ""%lu2%ct4%qf1 SQ. FT."">%", NStr) End If Next End Using Next addblock() End Sub Public Sub addblock() Using (AcDoc.LockDocument()) Using OpenDb As New Database(False, True) OpenDb.ReadDwgFile("\\ba-server\cad config\Forum\Library-Block\0000-Symbols\AREA BLOCK-X.dwg", FileShare.ReadWrite, True, "") Dim ids As New ObjectIdCollection() Using tr As Transaction = OpenDb.TransactionManager.StartTransaction() Dim bt As BlockTable bt = DirectCast(tr.GetObject(OpenDb.BlockTableId, OpenMode.ForRead), BlockTable) If bt.Has("AREA BLOCK") Then ids.Add(bt("AREA BLOCK")) End If tr.Commit() End Using If ids.Count <> 0 Then Dim destdb As Database = AcDoc.Database Dim iMap As New IdMapping() destdb.WblockCloneObjects(ids, destdb.BlockTableId, iMap, DuplicateRecordCloning.Ignore, False) End If End Using End Using Core.Application.DocumentManager.MdiActiveDocument = AcDoc InsertBlock() End Sub Private Sub InsertBlock() Using (AcDoc.LockDocument()) Using AcTrans As Transaction = AcDb.TransactionManager.StartTransaction() Dim BlkTab As BlockTable = DirectCast(AcTrans.GetObject(AcDb.BlockTableId, OpenMode.ForRead), BlockTable) If BlkTab.Has("AREA BLOCK") Then Dim BlkTabRec As BlockTableRecord = DirectCast(AcTrans.GetObject(BlkTab("AREA BLOCK"), OpenMode.ForRead), BlockTableRecord) Using BlkRef As New BlockReference(Point3d.Origin, BlkTab("AREA BLOCK")) BlkRef.TransformBy(AcEd.CurrentUserCoordinateSystem) Dim curspace As BlockTableRecord = DirectCast(AcTrans.GetObject(AcDb.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord) curspace.AppendEntity(BlkRef) AcTrans.AddNewlyCreatedDBObject(BlkRef, True) If BlkTabRec.HasAttributeDefinitions Then Dim attInfos As New Dictionary(Of String, TextInfo)() Dim attribClass As RXClass = RXObject.GetClass(GetType(AttributeDefinition)) For Each id As ObjectId In BlkTabRec Dim obj As DBObject = id.GetObject(OpenMode.ForRead) Dim attdef As AttributeDefinition = TryCast(obj, AttributeDefinition) If (attdef IsNot Nothing) AndAlso (Not attdef.Constant) Then Using attref As New AttributeReference BlkRef.AttributeCollection.AppendAttribute(attref) attref.SetAttributeFromBlock(attdef, BlkRef.BlockTransform) AcTrans.AddNewlyCreatedDBObject(attref, True) Dim fldstr As String If attref.Tag = "AREA-GROSS" Then fldstr = GStr ElseIf attref.Tag = "AREA-AC" Then fldstr = AStr ElseIf attref.Tag = "AREA-NONAC" Then fldstr = NStr End If Dim fld As Field = New Field(fldstr) fld.SetFieldCode(fldstr) fld.Evaluate() attref.SetField(fld) AcTrans.AddNewlyCreatedDBObject(fld, True) End Using End If Next End If End Using Else Core.Application.ShowAlertDialog(String.Format("Block '{0}' not found.", "AREA BLOCK")) End If AcTrans.Commit() End Using Pieces.Reset(Origlayer, Origosmode, Origorthomode, OrigSnapAngle, OrigDimScale) End Using End Sub Private Function GetEntitiesOnLayer(layerName As String) As ObjectIdCollection ' Build a filter list so that only entities on the specified layer are selected Dim tvs As TypedValue() = New TypedValue(1) {} tvs(0) = New TypedValue(CInt(DxfCode.LayerName), layerName) tvs(1) = New TypedValue(DxfCode.Start, "LWPOLYLINE") Dim sf As New SelectionFilter(tvs) Dim psr As PromptSelectionResult = AcEd.SelectAll(sf) If psr.Status = PromptStatus.OK Then Return New ObjectIdCollection(psr.Value.GetObjectIds()) Else Return New ObjectIdCollection() End If End Function End Class
Solved! Go to Solution.