Insert Block Using Named View's Center
I am trying to use VBA code to insert different blocks with their insertion point being based upon the center of a named view. I can insert a block with a hard coded insertion point fine, and I have previous named views created by using the view manager to select the boundaries of my pre-drawn rectangles. My problem is apparently I don't understand how the named view centers work.
UCS coordinates are world.
This is all being done in modelspace.
I already have about forty rectangles (size "32x18") set up in modelspace and each rectangle has a named view: Sheet1, Sheet2,......, Sheet40
Goal:
Insert block ABC with the insertion point of named view Sheet1's actual center, insert block XYZ with the insertion point of named view Sheet2's actual center, so on and so forth....
The reference point could be the top left corner of the named view for all I care, I just want static point that is at the same reference location for each named view.
Confusion:
If was to start a line from the center of Sheet1's rectangle it has an actual center location of 17.9556, 9.1380. If I look at the properties of the named view Sheet1, it has the center point of -457.8247, -3.4866. Huh?
I tried with code to change Sheet1's center point to the actual center of the rectangle, and it total changed where the view references. I would think that the center of the view would be the center of the boundaries selected.
I see that there is Camera and Target properties of named views, but google can't seem to answer the differences between them and the center property.
Long story short:
How can I insert a block into a named view's reference point, where the reference point is the actual center of the view?
Re: Insert Block Using Named View's Center
Welcome to AUGI. Post your code so we can help you. In the reply window, click on Go Advanced and use the code tags button to wrap your code.
Re: Insert Block Using Named View's Center
If you look at the "view" using a bit of lisp the centre is assoc 10
Code:
(setq tab (tblnext "view" T))
((0 . "VIEW") (2 . "view1") (70 . 0) (40 . 302.94) (10 210.0 148.5) (41 . 723.610236966825) (11 0.0 0.0 1.0) (12 0.0 0.0 0.0) (42 . 50.0) (43 . 0.0) (44 . 0.0) (50 . 0.0) (71 . 0) (72 . 1) (110 0.0 0.0 0.0) (111 1.0 0.0 0.0) (112 0.0 1.0 0.0) (79 . 0) (79 . 0) (146 . 0))
so closer to VBA using VL exposes property centre
Code:
(vlax-dump-object (vlax-ename->vla-object (tblobjname "VIEW" "view1")))
;
; Property values :
;
; Application (RO) = #<VLA-OBJECT IAcadApplication 000000002C20C400>
; CategoryName = ""
; Center = (210.0 148.5)
; Name = "view1"
Re: Insert Block Using Named View's Center
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?
Re: Insert Block Using Named View's Center
Are you trying to insert the block at View.Center or change/create a view? Naturally, if you change the view's center you will change the projection of the view.