Jump to content

Recommended Posts

Posted

Hi guys,

I was wondering if anyone can help me out on this one.

I have a vba lisp routine and i dont really know anything about it, just want to run this code in my auto cad, if anyone can give me step by step instructions on how to run this code in my autocad, I dont know anything about lisp so please give me complete instruction till i get it running and using in my autocad, thankyou

The code is as follows:

 

Option Explicit
Function GetActiveSpace() As AcadBlock
   With ThisDrawing
       If .ActiveSpace = acModelSpace Then
           Set GetActiveSpace = .ModelSpace
       Else
           Set GetActiveSpace = .PaperSpace
       End If
       End With
   End Function
Function StoreLayerStatus() As Variant
   Dim oLayer As AcadLayer
   Dim oLayers As AcadLayers
   Dim i As Integer
   With ThisDrawing
       Set oLayers = .Layers
   End With
   ReDim layerArr(0 To oLayers.Count - 1, 0 To 2) As Variant
   For Each oLayer In oLayers
       layerArr(i, 0) = oLayer.Name
       layerArr(i, 1) = oLayer.Lock
       layerArr(i, 2) = oLayer.Freeze
       On Error Resume Next
       oLayer.Freeze = False
       If Err Then Err.Clear
       i = i + 1
   Next oLayer
   On Error GoTo 0
   StoreLayerStatus = layerArr
End Function
Private Sub RestoreLayerStatus(ByVal ar As Variant)
   Dim oLayer As AcadLayer
   Dim oLayers As AcadLayers
   Dim i As Integer
       With ThisDrawing
       Set oLayers = .Layers
   End With
   For i = LBound(ar, 1) To UBound(ar, 1)
   Set oLayer = oLayers(ar(i, 0))
   On Error Resume Next '<-- to bypass active layer
   With oLayer
   .Lock = ar(i, 1)
   .Freeze = ar(i, 2)
   End With
   If Err Then Err.Clear
   Next i
   On Error GoTo 0
   
End Sub

Sub BlockReplace()
Dim lsArr As Variant
lsArr = StoreLayerStatus
Dim oSpace As AcadBlock
Dim varPt As Variant
Dim oEnt As AcadEntity
Dim oldBlkRef As AcadBlockReference
Dim newBlkRef As AcadBlockReference
Dim oldblkName As String
Dim newblkName As String
Dim layName As String
Dim xscl As Double
Dim yscl As Double
Dim zscl As Double
Dim rot As Double
Dim insPt(2) As Double
Dim ftype(0) As Integer
Dim fdata(0) As Variant
Dim dxfCode, dxfValue
Dim oSset As AcadSelectionSet
ThisDrawing.Utility.GetEntity oEnt, varPt, vbCrLf & "Select new block instance to replace with:"
If Not TypeOf oEnt Is AcadBlockReference Then
MsgBox "Selected object isn't a block reference"
Exit Sub
End If
Set newBlkRef = oEnt
If newBlkRef.IsDynamicBlock Then
newblkName = newBlkRef.EffectiveName
Else
newblkName = newBlkRef.Name
End If
         With ThisDrawing.SelectionSets
              While .Count > 0
                   .Item(0).Delete
              Wend
         Set oSset = .Add("$ReplaceBlocks$")
         End With
         
ftype(0) = 0
fdata(0) = "INSERT"
dxfCode = ftype: dxfValue = fdata
oSset.SelectOnScreen dxfCode, dxfValue
Set oSpace = GetActiveSpace
For Each oEnt In oSset
Set oldBlkRef = oEnt
With oldBlkRef
layName = .Layer
xscl = .XScaleFactor
yscl = .YScaleFactor
zscl = .ZScaleFactor
rot = .Rotation
varPt = .InsertionPoint
insPt(0) = varPt(0): insPt(1) = varPt(1): insPt(2) = varPt(2)
.Delete
End With
Set newBlkRef = oSpace.InsertBlock(insPt, newblkName, xscl, yscl, zscl, rot)
newBlkRef.Layer = layName
Next oEnt
RestoreLayerStatus (lsArr)
ThisDrawing.Regen acActiveViewport
End Sub

Posted

Sorry guys forgot to tell u that this code is for replacing a PARTICULAR instance of a block with another block.

Posted

try this explanation

 

VBA stands for Visual BASIC for Applications and is a Microsoft package available for many applications such as Excel. Most MS Office packages come with VBA and they are loosely based on Visual BASIC and while broadly similar are not identical.

 

A VBA routine can look something like this, this is a routine to scale blocks drawn with metric units to imperial units.

 

Public Sub ScaleRef()
Dim myObject As AcadEntity
Dim myScale As Double
myScale = 1 / 25.4
ThisDrawing.Utility.GetEntity myObject, basePnt, "Select an object"
If myObject.ObjectName = "AcDbBlockReference*" Then
   basePnt = myObject.InsertionPoint
   End If
   
myObject.XScaleFactor = myScale
myObject.YScaleFactor = myScale
myObject.ZScaleFactor = myScale
End Sub

 

To load the routine, first start up AutoCAD.

Select Tools > Macro > Visual Basic Editor

 

This will bring up the Visual Basic Editor screen. All routines are built here but if no routines are loaded it should be quite plain.

 

Select Insert > Module and a blank code window appears, either write a new VBA routine or paste an already written routine here.

 

Note the first line, in this code it is Public Sub ScaleRef. This means that ScaleRef is the name that is used to activate the routine.

 

In the Visual Basic editor window to the left is a project explorer window - if multiple routines are going to be used it is recommended to change the names of the routines from the default Module1 to something more descriptive. This is done by entering a new name in the Properties box, underneath the project explorer window.

 

Select File > Close and return to AutoCAD.

 

Once back in AutoCAD the routine is ready to be run, this is done in two steps, first a command (vbastmt) to active VBA and then the particular routine is activated.

 

For this routine, type the following:

 

vbastmt

at the prompt Expression: type ScaleRef

 

Now the prompt Select an object: appears and when a block is selected it is automatically scaled from metric to imperial.

 

When exiting AutoCAD, a dialogue box pops up asking if changes to the VBA project "Global1" should be saved, click the Yes-button. It should be saved as ACAD.dvb and saved in the Support folder. Doing this means that the routine will be loaded each time AutoCAD starts. If it is saved under another name and/or in another location, it will need to be loaded each time using the VBA Manager.

 

The VBA Manager is found under Tools > Macro. Click Load and browse to the location of the file

Posted
Hi guys,

I have a vba lisp routine

 

VBA and Lisp are two different things.

Your code is VBA, not lisp.

 

Looks like Tiger gave you the steps to run this code...

Posted

Thankyou Tiger,

Can u explain me what is the word used to activate my lisp routine like the one in your example.

Posted

Yeah my code is vba, can someone give examply using the code i have given?? I appreciate tiger's answer but it is still confusing for me to run my code as its different to the one tiger has used in his example, thankyou

Posted
Yeah my code is vba, can someone give examply using the code i have given?? I appreciate tiger's answer but it is still confusing for me to run my code as its different to the one tiger has used in his example, thankyou

 

Since I know next to nothing about VBA-routines, I can't help your there. I will move this thread to the Customization-forum, you might get a faster answer there.

Posted
I appreciate tiger's answer but it is still confusing for me to run my code as its different to the one tiger has used in his example,

 

The actual code doesn't matter. The process that Tiger outlined for you is the same, just use your code. The only difference I might mention is that instead of using the "vbastmt" command at the command line, you can run the VBARUN command and choose the macro from a dialog.

 

Note: if you are running AutoCAD 2010, then VBA is not included. You can download it from Autodesk though.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...