View Full Version : Deleting Block using VBA..??
johnbest5673679539
2011-05-10, 11:59 AM
Dear friends
My drawing needs to plot arcs, so I hv written a VBA code to plot the arcs. This I hv to test on various points in the drawing. So just running the macro does the required.
But now when it comes to delete these arcs, I hv to either use UNDO (Ctrl+Z) number of times. So I created a block and then plotted the arcs. Selecting any of the arc and pressing delete, deletes the complete set of ARCS (ie those arcs which I plot).
But a strange thing happened when I plot the arcs second time. Second time when I plotted the arcs on different points, I saw the previous arc too.. So on everytime I plot arcs, all the previous arcs are present in the block, WHICH I DONT WANT...:(
I thought deleting the Block everytime i run and then inserting it again, using the same code would be a best solution. But unable to do the same, being a newbie to both autocad and vba..:cry:
Can some body help me out with the solution I said above.. or another solution to suit my requirement?
have a nice day
johnbest
BlackBox
2011-05-10, 12:56 PM
To answer the question of 'how to delete objects'... simply use the Delete Method.
However, I'd ask you to provide a more clear description of what it is you're doing, as it initially sounds cumbersome. Perhaps there's a better way to accomplish what you want, but I would need more information.
johnbest5673679539
2011-05-11, 06:05 AM
Dear RenderMan,
I have some drawing on which I hv to plot some arcs for testing on various points. After testing I dont need these arcs. There are 5 arcs and 2 lines needed for the testing. After testing, as there is no need of these arcs, I hv to delete them manually. Say I plot these arcs on 10 points, So I have to delete 50 arcs and 20 lines, which is quite tiresome and tedious job.
So I thought, a block would help me to do this job by simply deleting the block all the pplotted arcs would get deleted. This worked once. Thereafter, when I plot these arcs on next 10 points, the previous 10, along with new 10 gets plotted, which I dont need as the previous 10 points are tested.
So How to delete the previous arcs in that block? Everytime I plot the arcs, previous arcs even after deleting them gets plotted, which I dont want.
I think I have explained it very clearly. :)
Waiting for you reply and guidance :)
have a nice day
john Best
BlackBox
2011-05-11, 01:02 PM
I think I have explained it very clearly. :)
Not enough for me to help you.
Waiting for you reply and guidance :)
Here's my guidance (again):
... use the Delete Method.
This is a programming forum... If you need more guidance than that, I would suggest that you post your code (and your block?).
Ed Jobe
2011-05-11, 03:12 PM
As you add the arcs to the dwg, add them to a Selectionset object, then use the ss's Erase method.
jwanstaett
2011-05-11, 07:29 PM
sound like you are drawing the arcs in a block then inserting the block in the drawing. the next time you run the program you add the new arcs to the block using the same block name. be for you add the new arcs to the block you need to delete the old arcs in the block definition. When you delete inserted block form the drawing the block definition is not deleted only the inserted blockref.
Yes you need to post your code for more help
Ed Jobe
2011-05-11, 09:26 PM
sound like you are drawing the arcs in a block then inserting the block in the drawing. the next time you run the program you add the new arcs to the block using the same block name. be for you add the new arcs to the block you need to delete the old arcs in the block definition. When you delete inserted block form the drawing the block definition is not deleted only the inserted blockref.
Yes you need to post your code for more help
You're using the block as a grab bag to store stuff in. Using dwg ents to do this is overkill, just use code to store references to the stuff you've added. An added benefit is that you don't have to select the block. Temp objects should always be stored in memory, not the dwg database.
BlackBox
2011-05-11, 09:30 PM
Show me the [code]!
http://www.ppc-advice.com/wp-content/uploads/2009/06/moneyvote-150x150.jpg
johnbest5673679539
2011-05-12, 05:47 AM
Dear friends,
Below is the code...
Sub PlotArcs()
Dim lineLen As Double
Dim cent1(0 To 2) As Double
Dim cent2(0 To 2) As Double
Dim r1(0 To 2) As Double
Dim r2(0 To 2) As Double
Dim stPt(0 To 2) As Double
Dim enPt(0 To 2) As Double
Dim intPt As Variant
Dim intPt1(0 To 2) As Double
Dim intPt2(0 To 2) As Double
Dim v As Variant
Dim circle1 As AcadCircle
Dim circle2 As AcadCircle
Dim radLen As Double
Dim nLine As AcadLine
Dim cirCol As AcadAcCmColor
Dim verCol As AcadAcCmColor
Dim ang As Double
'Now plotting the Outer Arc
Dim arcObj As AcadArc
Dim startAngleInRadian As Double
Dim endAngleInRadian As Double
' Create the block
Dim blockObj As AcadBlock
Dim insertionPnt(0 To 2) As Double
Dim ent As AcadEntity
Dim blockRefObj As AcadBlockReference
'First deleting the blocks then inserting again
For Each ent In ThisDrawing.ModelSpace
If TypeOf ent Is AcadBlockReference Then
Set blockRefObj = ent
If blockRefObj.Name = "PlotARCBlock" Then
blockRefObj.Delete
End If
End If
'End With
Next
'Inserting the Blocks
insertionPnt(0) = 0#
insertionPnt(1) = 0#
insertionPnt(2) = 0#
Set blockObj = ThisDrawing.Blocks.Add(insertionPnt, "PlotARCBlock")
Set verCol = AcadApplication.GetInterfaceObject("AutoCAD.AcCmColor.16")
Call verCol.SetRGB(255, 10, 255)
Set cirCol = AcadApplication.GetInterfaceObject("AutoCAD.AcCmColor.16")
Call cirCol.SetRGB(91, 91, 91)
v = ThisDrawing.Utility.GetPoint(, "First Circle Center Point... ")
cent1(0) = v(0)
cent1(1) = v(1)
cent1(2) = v(2)
v = ThisDrawing.Utility.GetPoint(, "Select the Radius Point for First Circle... ")
r1(0) = v(0)
r1(1) = v(1)
r1(2) = v(2)
Set nLine = blockObj.AddLine(cent1, r1)
radLen = nLine.Length
Set circle1 = blockObj.AddCircle(cent1, radLen)
'Set circle1 = ModelSpace.AddCircle(cent1, radLen)
circle1.TrueColor = cirCol
Set circle2 = blockObj.AddCircle(r1, radLen)
'Set circle2 = ModelSpace.AddCircle(r1, radLen)
circle2.TrueColor = cirCol
intPt = circle1.IntersectWith(circle2, acExtendNone)
' Print all the intersection points
Dim I As Integer, j As Integer, k As Integer
If VarType(intPt) <> vbEmpty Then
For I = LBound(intPt) To UBound(intPt)
If k = 0 Then
intPt1(0) = intPt(j)
intPt1(1) = intPt(j + 1)
intPt1(2) = intPt(j + 2)
'MsgBox "First Point - " & intPt1(0) & ", " & intPt1(1)
ElseIf k = 1 Then
intPt2(0) = intPt(j)
intPt2(1) = intPt(j + 1)
intPt2(2) = intPt(j + 2)
'MsgBox "Second Point - " & intPt2(0) & ", " & intPt2(1)
End If
I = I + 2
j = j + 3
k = k + 1
Next
End If
Set nLine = blockObj.AddLine(intPt2, intPt1)
'Set nLine = ModelSpace.AddLine(intPt2, intPt1)
lineLen = nLine.Length
nLine.TrueColor = cirCol
ang = ThisDrawing.Utility.AngleFromXAxis(intPt1, intPt2)
startAngleInRadian = 270 * 3.141592 / 180#
endAngleInRadian = 90 * 3.141592 / 180#
Set arcObj = blockObj.AddArc(cent1, lineLen, startAngleInRadian, endAngleInRadian)
'Set arcObj = ModelSpace.AddArc(cent1, lineLen, startAngleInRadian, endAngleInRadian)
arcObj.TrueColor = verCol
' Insert the block
insertionPnt(0) = 0#: insertionPnt(1) = 0#: insertionPnt(2) = 0
Set blockRefObj = ThisDrawing.ModelSpace.InsertBlock(insertionPnt, "PlotARCBlock", 1#, 1#, 1#, 0)
End Sub
Have a nice day
John
Ed Jobe
2011-05-12, 03:38 PM
First, Dim and AcadSelectionSet obj using this:
Public Function AddSelectionSet(SetName As String) As AcadSelectionSet
' This routine does the error trapping neccessary for when you want to create a
' selectin set. It takes the proposed name and either adds it to the selectionsets
' collection or sets it.
On Error Resume Next
Set AddSelectionSet = ThisDrawing.SelectionSets.Add(SetName)
If Err.Number <> 0 Then
Set AddSelectionSet = ThisDrawing.SelectionSets.Item(SetName)
AddSelectionSet.Clear
End If
End Function
Then, uncomment your line to set nline from a line added to ms. Then add nline to the ss. Do the same for your arcs. Then get rid of all the block code. When you're done with the temp ents, use ss.Erase()
As to why your code didn't get rid of prev arcs, don't forget what you first learned about blocks. You create a definition and then insert references to the definition. The corresponding vba objects are AcadBlock and AcadBlockReference. When you do your deletion, you are only selecting BlockRef's. The PURGE command deletes the block def. In terms of the dwg database, first you have to delete the references to the defnition from the modelspace table, then you can delete the def from the Block table.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.