viewport layer not working

viewport layer not working

Anonymous
Not applicable
510 Views
8 Replies
Message 1 of 9

viewport layer not working

Anonymous
Not applicable
I'm trying to set an override to the viewport command that will put the viewport onto layer "0cm-Annot-Viewports". The code below doesn't set the layer correctly- what's wrong?

Dim objLayer As AcadLayer
Dim strLayerName As String
Dim objOrigLayer As AcadLayer

Set objOrigLayer = ThisDrawing.ActiveLayer
strLayerName = ("0cm-Annot-VPorts")
Set objLayer = ThisDrawing.Layers.Add(strLayerName)
objLayer.color = 251

If ThisDrawing.Layers.Item("0cm-Annot-VPorts").Freeze = True Then
ThisDrawing.Layers.Item("0cm-Annot-VPorts").Freeze = False
ElseIf ThisDrawing.Layers.Item("0cm-Annot-VPorts").LayerOn = False Then
ThisDrawing.Layers.Item("0cm-Annot-VPorts").LayerOn = True
End If
ThisDrawing.ActiveLayer = objLayer: ThisDrawing.SendCommand "_regen" & vbCr
ThisDrawing.SendCommand "_-vports" & vbCr: ThisDrawing.SendCommand "_regen" & vbCr
ThisDrawing.ActiveLayer = objOrigLayer
0 Likes
511 Views
8 Replies
Replies (8)
Message 2 of 9

Anonymous
Not applicable
Get rid of those send commands then try it.
Thisdrawing.regen,,,,,,
0 Likes
Message 3 of 9

Anonymous
Not applicable
OK, I've tried to replace the "send commands" but still get an execution error with the following:

Dim objVp As AcadPViewport
Dim dblCenter(0 To 2) As Double
Dim dblWidth As Double
Dim dblHeight As Double
Dim varPnt As Variant: Dim varPnt2 As Variant

varPnt = ThisDrawing.Utility.GetPoint(, "Please Select First Point") 'Gets User Insertion Point
varPnt2 = ThisDrawing.Utility.GetPoint(, "Please Select Second Point")

ReDim varPnt(0 To 2) As Double: ReDim varPnt2(0 To 2) As Double
dblWidth = varPnt2(0) - varPnt(0): dblHeight = varPnt2(1) - varPnt(1)
dblCenter(0) = varPnt2(0) - varPnt(0): dblCenter(1) = varPnt2(1) - varPnt(1): dblCenter(2) = 0

Set objVp = ThisDrawing.PaperSpace.AddPViewport(dblCenter, dblWidth, dblHeight)
With objVp
.Display (True)
.ViewportOn = True
End With

ThisDrawing.Regen acAllViewports
0 Likes
Message 4 of 9

Anonymous
Not applicable
varPnt2 = ThisDrawing.Utility.GetPoint(varPnt, "Please Select Second Point")

Redim isn't needed here as the variant is already an array of doubles, infact you are setting all the double values to zero
'ReDim varPnt(0 To 2) As Double: ReDim varPnt2(0 To 2) As Double
0 Likes
Message 5 of 9

Anonymous
Not applicable
Ok, I've removed the ReDim statements but I still can't get the routine to work 😞
0 Likes
Message 6 of 9

Anonymous
Not applicable
It worked for me, Print your sub so we can look at it.
0 Likes
Message 7 of 9

Anonymous
Not applicable
Ah, got it working! Apologies, I worked out the coordinates incorrectly before (hence the execution error). The code below works fine - it is started through a lisp that overides my ViewPort command in AutoCAD. The only downfall is that it doesn't show the dashed lines after selecting varPnt and before varPnt2.

Public Sub OcmViewports()
Dim objLayer As AcadLayer
Dim strLayerName As String
Dim objOrigLayer As AcadLayer

Set objOrigLayer = ThisDrawing.ActiveLayer
strLayerName = ("0cm-Annot-VPorts")
Set objLayer = ThisDrawing.Layers.Add(strLayerName)
objLayer.color = 251

If ThisDrawing.Layers.Item("0cm-Annot-VPorts").Freeze = True Then
ThisDrawing.Layers.Item("0cm-Annot-VPorts").Freeze = False
ElseIf ThisDrawing.Layers.Item("0cm-Annot-VPorts").LayerOn = False Then
ThisDrawing.Layers.Item("0cm-Annot-VPorts").LayerOn = True
End If

ThisDrawing.ActiveLayer = objLayer

Dim objVp As AcadPViewport
Dim dblCenter(0 To 2) As Double
Dim dblWidth As Double
Dim dblHeight As Double
Dim varPnt As Variant: Dim varPnt2 As Variant

varPnt = ThisDrawing.Utility.GetPoint(, "Please Select First Point") 'Gets User Insertion Point
varPnt2 = ThisDrawing.Utility.GetPoint(, "Please Select Second Point")

If varPnt(0) < varPnt2(0) Then dblWidth = varPnt2(0) - varPnt(0) Else dblWidth = varPnt(0) - varPnt2(0)

If varPnt(1) < varPnt2(1) Then dblHeight = varPnt2(1) - varPnt(1) Else dblHeight = varPnt(1) - varPnt2(1)

dblCenter(0) = (varPnt2(0) + varPnt(0)) / 2: dblCenter(1) = (varPnt2(1) + varPnt(1)) / 2: dblCenter(2) = 0

Set objVp = ThisDrawing.PaperSpace.AddPViewport(dblCenter, dblWidth, dblHeight)
With objVp
.Display (True)
.ViewportOn = True
End With

ThisDrawing.Regen acAllViewports
ThisDrawing.ActiveLayer = objOrigLayer
End Sub
0 Likes
Message 8 of 9

Anonymous
Not applicable
Glad it's working, I did post the fix for the "Dashed lines".
ThisDrawing.Layers.Item("0cm-Annot-VPorts").Freeze is the same as objLayer.Freeze.
0 Likes
Message 9 of 9

Anonymous
Not applicable
I use a command reactor. If the user issue's either a VPORTS or MVIEW command I change the layer to the layer I want before the command is issued and then change it back to the current layer after the command is issued.
0 Likes