Create a view in several layout by user

Create a view in several layout by user

ossareh.hamidreza
Explorer Explorer
446 Views
3 Replies
Message 1 of 4

Create a view in several layout by user

ossareh.hamidreza
Explorer
Explorer

Dear All,

 

I hope all is well with you. I try to write a VBA code in AutoCAD to :

1- Create a requested number of layouts from user by user form and create them

2- Delete all current objects from each created layout

3- The user selects its first view and put it in layout 1, then selects the second view and put it in layout 2, .......

 

Please review the current code: When you run, it works up to step two above correctly and does well in step three but only for one layout, and it does not continue its job up to the end one. I will be highly appreciated it if someone helps me.

 

Codes:

 

Public explicity
Dim objAcadObject As AcadObject
Dim NumberofLayouts As Integer, NumberofRequired As Integer


Private Sub DoCreatelayout()

Dim currentLayout As AcadLayout
Dim LayoutName As String
'Dim NumberofLayouts As Integer, NumberofRequired As Integer
Dim Count As Integer
Dim entry As AcadEntity

ThisDrawing.ActiveSpace = acPaperSpace

NumberofRequired = UserForm1.TextBox1.Text

For Each currentLayout In ThisDrawing.Layouts

LayoutName = currentLayout.Name

If LayoutName <> "Model" Then currentLayout.Delete

Next

ThisDrawing.ActiveLayout = ThisDrawing.Layouts("Layout1")

For Each objAcadObject In ThisDrawing.PaperSpace

If TypeName(objAcadObject) = "IAcadPViewport" Then objAcadObject.Delete

Next

For Count = 2 To NumberofRequired

ThisDrawing.Layouts.Add ("Layout" & Count)

ThisDrawing.ActiveLayout = ThisDrawing.Layouts("Layout" & Count)

For Each objAcadObject In ThisDrawing.PaperSpace

If TypeName(objAcadObject) = "IAcadPViewport" Then objAcadObject.Delete

Next

Next

ThisDrawing.Regen acActiveViewport

End Sub

Private Sub CommandButton1_Click()

DoCreatelayout

UserForm1.Hide

For Count = 2 To NumberofRequired


ThisDrawing.ActiveLayout = ThisDrawing.Layouts("Layout" & Count)

ThisDrawing.SendCommand "mview" & vbCr & "NEW" & vbCr

Next

 

End Sub

 

0 Likes
447 Views
3 Replies
Replies (3)
Message 2 of 4

Ed__Jobe
Mentor
Mentor

Welcome to the VBA forum. When posting code, put your code into the code window by clicking on the </> button. It should look like this:

 

 

Sub MySub()

End Sub

 

 

 

The problem lies in using the SendCommand method. It is not synchronous, i.e. is runs after the VBA code is finished. Therefore its not in a loop and only runs once. Use the VBA API to add entities instead of AutoCAD commands. Use ThisDrawing.Viewports.Add() and then set the viewport properties.

 

 

Dim oVP As AcadViewport
Set oVP = ThisDrawing.Viewports.Add
With oVP
  'set properties
  .LowerLeftCorner = point1
  .UpperRightCorner = point2
  .Layer = "Viewports" 'make sure layer exists first
End With

 

 

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 3 of 4

ossareh.hamidreza
Explorer
Explorer

Dear Sir\Madam,

 

So many thanks for your kind advice. In your submitted code, for the viewport property, I would like to let the user to select his views by using window action. are there any specific commands to ask the user to do that?

Thanks in advance.

For example in:

Set PViewportObject = PaperSpaceCollection.AddPViewport(CenterPoint, _

Width, Height)

Instead of CenterPoint, width, and height parameters, how is it possible that the user selects the viewport by a window in model space. 

Regards

0 Likes
Message 4 of 4

Ed__Jobe
Mentor
Mentor

@ossareh.hamidreza wrote:

Dear Sir\Madam,

It's Sir Ed. 🙂

 

Have a look at the Utility object's methods. You can use GetPoint twice. Search this forum for sample code.

 

Note that I've been linking to the ActiveX Developer Reference. It's acadauto.chm on your hd.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes