FYI, I am using Excel VBA to manipulate CAD. If you are using this only in AutoCAD VBA direct, change "acad" in the code to "ThisDrawing".
Code for changing view's center:
Code:
Public acad As AcadApplication
Public acadDoc As AcadDocument
Sub EditViewCenter()
Dim viewObj As AcadView
Dim ctr(1) As Double
'*******************************************************************************
'If using VBA directly in AutoCAD, remove this little section here. This is for using Excel VBA with AutoCAD.
On Error Resume Next
Set acad = GetObject(, "AutoCAD.application")
If Err <> 0 Then
Err.Clear
Set acad = CreateObject("AutoCAD.application")
If Err <> 0 Then
MsgBox "Could not start AutoCAD", vbExclamation
End
End If
End If
acad.Visible = True
'Set the name of the opened drawing.
Set acadDoc = acad.Documents.Item("Create Layouts.dwg")
'*******************************************************************************
'Item 0 is for the named view Sheet1.
'I would like figure out how to use the actual name "Sheet1" in referencing the named view, but that's for another day..... baby steps.....
'Right now using Views.Item(0) works.
Set viewObj = acadDoc.Views.Item(0)
'Assign Center X and Y values to the actual center coordinates of the rectangle for the named view.
ctr(0) = 17.9556
ctr(1) = 9.138
'Update named view's center
viewObj.center = ctr
End Sub
This changes the view's center value to the actual center of the rectangle, but when I do that it changes the view's actual location to some other place inside the drawing. The named view is no longer around the rectangle previously selected.
Code for inserting block referencing the named view's center:
Code:
Sub CreateDIcenterView()
Dim insertionPoint(2) As Double
Dim acadBlockRef As AcadBlockReference
Dim blockName As String
Dim viewObj As AcadView
Set viewObj = acadDoc.Views.Item(0)
blockName = "DI"
'Assign insertion point of block from the named view's center
insertionPoint(0) = viewObj.Center(0)
insertionPoint(1) = viewObj.Center(1)
insertionPoint(2) = 0
Set acadBlockRef = acadDoc.ModelSpace.InsertBlock(insertionPoint, blockName, 1, 1, 1, 0)
End Sub
So..... back to my original questions:
Why does is the center of the named view not the actual center of the rectangle selected?
Why does changing the center of the named view to the actual center of the rectangle change the location of the named view?
Should I be using something different like the camera or target variables?