1、添加比例
 - <CommandMethod("AddScale")>
- Public Shared Sub addScale()
- Dim doc As Document = Application.DocumentManager.MdiActiveDocument
- Dim db As Database = doc.Database
- ' next get the objectContextManager
- Try
- Dim contextManager As ObjectContextManager = db.ObjectContextManager
- ' if ok
- If contextManager IsNot Nothing Then
- ' now get the Annotation Scaling context collection
- ' (named ACDB_ANNOTATIONSCALES_COLLECTION)
- Dim contextCollection As ObjectContextCollection = contextManager.GetContextCollection("ACDB_ANNOTATIONSCALES")
- ' if ok
- If contextCollection IsNot Nothing Then
- ' create a brand new scale context
- Dim annotationScale As New AnnotationScale With {
- .Name = "WBScale2 1:28",
- .PaperUnits = 1,
- .DrawingUnits = 28
- }
- ' now add to the drawing's context collection
- contextCollection.AddContext(annotationScale)
- End If
- End If
- Catch ex As System.Exception
- Dim ed As Editor = doc.Editor
- ed.WriteMessage(ex.ToString())
- End Try
- End Sub
代码转自:https://adndevblog.typepad.com/a ... e-to-a-drawing.html
2:删除英制比例等
 - <CommandMethod("DelScale")>
- Public Shared Sub DelScale()
- Dim doc As Document = Application.DocumentManager.MdiActiveDocument
- Dim db As Database = doc.Database
- ' next get the objectContextManager
- Try
- Dim contextManager As ObjectContextManager = db.ObjectContextManager
- ' if ok
- If contextManager IsNot Nothing Then
- ' now get the Annotation Scaling context collection
- ' (named ACDB_ANNOTATIONSCALES_COLLECTION)
- Dim contextCollection As ObjectContextCollection = contextManager.GetContextCollection("ACDB_ANNOTATIONSCALES")
- ' if ok
- If contextCollection IsNot Nothing Then
- For Each objectContext As ObjectContext In contextCollection
- If TypeOf objectContext Is AnnotationScale Then
- Dim Name As String = objectContext.Name
- If Name.Contains("=") Then
- '删除英制比例,比如: 1/128" = 1'-0"
- contextCollection.RemoveContext(Name)
- End If
- If Name.Contains(":") Then
- '删除公制比例中的2:1、4:1、8:1、10:1等
- Dim Start As String = Name.Split(":")(0)
- If Start <> "1" Then
- contextCollection.RemoveContext(Name)
- End If
- End If
- End If
- Next
- End If
- End If
- Catch ex As System.Exception
- Dim ed As Editor = doc.Editor
- ed.WriteMessage(ex.ToString())
- End Try
- End Sub
3、清理比例
 - <CommandMethod("PuScale")>
- Public Shared Sub PuScale()
- Dim doc As Document = Application.DocumentManager.MdiActiveDocument
- Dim db As Database = doc.Database
- Try
- Using acTr As Transaction = db.TransactionManager.StartTransaction()
- Dim objectIdCollection As New ObjectIdCollection()
- Dim objectContextManager As ObjectContextManager = db.ObjectContextManager
- If objectContextManager IsNot Nothing Then
- Dim contextCollection As ObjectContextCollection = objectContextManager.GetContextCollection("ACDB_ANNOTATIONSCALES")
- If contextCollection IsNot Nothing Then
- For Each objectContext As ObjectContext In contextCollection
- If TypeOf objectContext Is AnnotationScale Then
- objectIdCollection.Add(New ObjectId(objectContext.UniqueIdentifier))
- End If
- Next
- End If
- End If
- db.Purge(objectIdCollection)
- For Each obj As Object In objectIdCollection
- Dim objectId As ObjectId = CType(obj, ObjectId)
- acTr.GetObject(objectId, OpenMode.ForWrite).[Erase]()
- Next
- acTr.Commit()
- End Using
- Catch ex As System.Exception
- Dim ed As Editor = doc.Editor
- ed.WriteMessage(ex.ToString())
- End Try
- End Sub
|