Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Public NotInheritable Class CheckLevels
Public Shared Sub CheckLevel()
Dim Unit As Double = Val(GetSetting("AMR LISP", "Check Level", "Unit", 1000))
Dim Method As String = GetSetting("AMR LISP", "Check Level", "Method", "Custom")
Dim LFormat As String = GetSetting("AMR LISP", "Check Level", "Format", "0.00")
Dim LastMethod As String = GetSetting("AMR LISP", "Check Level", "LastMethod", "Change")
Dim BPoint As Point3d, BValue As Double, BlockName As String
Dim Trans As Transaction = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction()
Dim AcEntOption As PromptEntityOptions = New PromptEntityOptions(vbLf & "Select base block")
AcEntOption.Keywords.Add("Unit")
AcEntOption.Keywords.Add("Method")
AcEntOption.Keywords.Add("Format")
AcEntOption.SetRejectMessage(vbLf & "You must pick a block try again ...")
AcEntOption.AddAllowedClass(GetType(BlockReference), False)
L1: Dim AcEntResult As PromptEntityResult = Application.DocumentManager.MdiActiveDocument.Editor.GetEntity(AcEntOption)
If AcEntResult.Status = PromptStatus.OK Then
Dim BlkRef As BlockReference = Trans.GetObject(AcEntResult.ObjectId, OpenMode.ForRead)
If (BlkRef.AttributeCollection.Count) > 0 Then
Dim objId As ObjectId = BlkRef.AttributeCollection.Item(0)
Dim attRef As AttributeReference = Trans.GetObject(objId, OpenMode.ForRead)
BValue = Val(attRef.TextString)
BlockName = BlkRef.Name
BPoint = New Point3d(BlkRef.Position.X, BlkRef.Position.Y, BlkRef.Position.Z)
Else
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(vbLf & "you are selected invalid block doesn't have any attributes")
Exit Sub
End If
' start select blocks which we want to edit it
' create selection filter with typedvalues
Dim TypedValues(0) As TypedValue
TypedValues.SetValue(New TypedValue(DxfCode.BlockName, BlockName), 0)
Dim AcSelFtr As SelectionFilter = New SelectionFilter(TypedValues)
'create keywords options for selection
Dim AcSelOption As PromptSelectionOptions = New PromptSelectionOptions
AcSelOption.MessageForAdding = ("Select other (" & BlockName & ") blocks that you want to change")
'start selectionset with keywords and filter
Dim AcSelResult As PromptSelectionResult
AcSelResult = Application.DocumentManager.MdiActiveDocument.Editor.GetSelection(AcSelOption, AcSelFtr)
If AcSelResult.Status = PromptStatus.OK Then
Select Case Method
Case "Change"
ChangeTextLevel(AcSelResult.Value, BPoint, BValue, Unit, LFormat)
Case "Move"
ChangePostionLevel(AcSelResult.Value, BPoint, BValue, Unit)
Case "Custom"
Dim AcKwrdOption As PromptKeywordOptions = New PromptKeywordOptions(vbLf & "Which method will execute [Change/Move] : ", "Change Move")
AcKwrdOption.Keywords.Default = LastMethod
Dim AcKwrdResult As PromptResult = Application.DocumentManager.MdiActiveDocument.Editor.GetKeywords(AcKwrdOption)
If AcKwrdResult.Status = PromptStatus.OK Then
Select Case AcKwrdResult.StringResult
Case "Change"
ChangeTextLevel(AcSelResult.Value, BPoint, BValue, Unit, LFormat)
Case "Move"
ChangePostionLevel(AcSelResult.Value, BPoint, BValue, Unit)
End Select
LastMethod = AcKwrdResult.StringResult
SaveSetting("AMR LISP", "Check Level", "LastMethod", LastMethod)
End If
End Select
End If
' end code
ElseIf AcEntResult.Status = PromptStatus.Keyword Then
Select Case AcEntResult.StringResult
Case "Unit"
Dim AcDblOption As PromptDoubleOptions = New PromptDoubleOptions(vbLf & "Enter the calculation unit (Meter Unit) ")
AcDblOption.AllowNone = False
AcDblOption.DefaultValue = Unit
AcDblOption.AllowNegative = False
Dim AcDblResult As PromptDoubleResult = Application.DocumentManager.MdiActiveDocument.Editor.GetDouble(AcDblOption)
If AcDblResult.Status = PromptStatus.OK Then
Unit = AcDblResult.Value
SaveSetting("AMR LISP", "Check Level", "Unit", Unit)
GoTo L1
End If
GoTo L1
Case "Method"
Dim AcKwrdOption As PromptKeywordOptions = New PromptKeywordOptions(vbLf & "Which method you will use [Change/Move/cuStom] : ", "Change Move Custom")
AcKwrdOption.Keywords.Default = Method
Dim AcKwrdResult As PromptResult = Application.DocumentManager.MdiActiveDocument.Editor.GetKeywords(AcKwrdOption)
If AcKwrdResult.Status = PromptStatus.OK Then
Method = AcKwrdResult.StringResult
SaveSetting("AMR LISP", "Check Level", "Method", Method)
End If
GoTo L1
Case "Format"
Dim AcStrOption As PromptStringOptions = New PromptStringOptions("Enter text format that you want :")
AcStrOption.DefaultValue = LFormat
Dim AcStrResult As PromptResult = Application.DocumentManager.MdiActiveDocument.Editor.GetString(AcStrOption)
If AcStrResult.Status = PromptStatus.OK Then
LFormat = AcStrResult.StringResult
SaveSetting("AMR LISP", "Check Level", "Format", LFormat)
End If
GoTo L1
End Select
End If
Trans.Commit()
Trans.Dispose()
End Sub
Private Shared Sub ChangeTextLevel(MySelSet As SelectionSet, BPoint As Point3d, BValue As String, Unit As Double, LFormat As String)
Dim Tr As Transaction = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction()
If MySelSet IsNot Nothing Then
For Each BlKID In MySelSet.GetObjectIds()
Dim BlkRef As BlockReference = Tr.GetObject(BlKID, OpenMode.ForWrite)
Dim objId As ObjectId = BlkRef.AttributeCollection.Item(0)
Dim attRef As AttributeReference = Tr.GetObject(objId, OpenMode.ForWrite)
If BlkRef.Position.Y > BPoint.Y Then
attRef.TextString = LevelFormat((BValue + (BlkRef.Position.Y - BPoint.Y) / Unit), LFormat)
ElseIf BlkRef.Position.Y < BPoint.Y Then
attRef.TextString = LevelFormat((BValue - (BPoint.Y - BlkRef.Position.Y) / Unit), LFormat)
ElseIf BlkRef.Position.Y = BPoint.Y Then
attRef.TextString = LevelFormat((BValue), LFormat)
End If
Next
End If
Tr.Commit()
Tr.Dispose()
End Sub
Private Shared Function LevelFormat(LValue As Double, LFormat As String) As String
If LValue > 0 Then
Return ("+" & Format(LValue, LFormat))
ElseIf LValue < 0 Then
Return (Format(LValue, LFormat))
ElseIf LValue = 0 Then
Return ("±" & Format(LValue, LFormat))
Else
Return ""
End If
End Function
Private Shared Sub ChangePostionLevel(MySelSet As SelectionSet, BPoint As Point3d, BValue As String, Unit As Double)
Dim Tr As Transaction = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction()
If MySelSet IsNot Nothing Then
For Each BlKID In MySelSet.GetObjectIds()
Dim BlkRef As BlockReference = Tr.GetObject(BlKID, OpenMode.ForWrite)
Dim objId As ObjectId = BlkRef.AttributeCollection.Item(0)
Dim attRef As AttributeReference = Tr.GetObject(objId, OpenMode.ForWrite)
Dim OPoint As Point3d = New Point3d(BlkRef.Position.X, BlkRef.Position.Y, BlkRef.Position.Z)
Dim NPoint As Point3d
If attRef.TextString > BValue Then
NPoint = New Point3d(BlkRef.Position.X, (BPoint.Y + (Val(attRef.TextString) - Val(BValue)) * Unit), BlkRef.Position.Z)
ElseIf attRef.TextString < BValue Then
NPoint = New Point3d(BlkRef.Position.X, (BPoint.Y - (Val(BValue) - Val(attRef.TextString)) * Unit), BlkRef.Position.Z)
ElseIf attRef.TextString = BValue Then
'Dim NPoint As Point3d = New Point3d(BlkRef.Position.X, (BPoint.Y + (Val(attRef.TextString) - Val(BValue)) * Unit), BlkRef.Position.Z)
End If
BlkRef.TransformBy(Matrix3d.Displacement(OPoint.GetVectorTo(NPoint)))
Next
End If
Tr.Commit()
Tr.Dispose()
End Sub
End Class this is my all code , and i attaced level block atrribute