create viewport from rectangle or block

create viewport from rectangle or block

angelinabrown0123
Advocate Advocate
8,041 Views
28 Replies
Message 1 of 29

create viewport from rectangle or block

angelinabrown0123
Advocate
Advocate

Hi All

I want to make viewport from rectangle or block, have to keep the scale of viewport 0.5, I don't know if it is possible with VBA code. If it is possible with VBA code then help me to provide the code.

Thank in advance

0 Likes
Accepted solutions (2)
8,042 Views
28 Replies
Replies (28)
Message 2 of 29

MakCADD
Advocate
Advocate

consider viewport from block

assuming the block inserted in paperspace is in scale= 1  & roation =0 in paperspace

getboundingbox

addviewport  with center,width and height from getboundingbox

change space to mspace

get midpoint of block in the modelspace using getboundingbox

set veiwctr systemvariable with midpoint

rotate view according to angle of block in modelspace

scale the viewport relative to block in modelspace.

 

i didnt try this,

i will try to code if i get time

 

another option

insert block with same rotation as in the modelspace

then explode and clip the viewport with pline inside the exploded block

then rotate the viewport using sendcommand to the -angle

 

 

0 Likes
Message 3 of 29

angelinabrown0123
Advocate
Advocate

@MakCADD ,

You have given very good logic but it is impossible for me to do it, however I am learning VBA right now if you can help me I will be very grateful to you please do it in your spare time I have no hurry

0 Likes
Message 4 of 29

grobnik
Collaborator
Collaborator

Hi @angelinabrown0123 , @MakCADD 

here below a link to a post made by EdJobe on the same topic

Solved: Re: GET THE COORDINATES OF A VIEWPORT - Autodesk Community - AutoCAD

0 Likes
Message 5 of 29

Ed__Jobe
Mentor
Mentor

While you can certainly write  your own code, the MVIEW command has this feature.

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 6 of 29

Ed__Jobe
Mentor
Mentor

It didn't add the screencast.

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 7 of 29

maratovich
Advisor
Advisor

Wouldn't it be better to use a ready-made solution?
Use the forum search, there are several options.
For example this - AutoViewport 

---------------------------------------------------------------------
Software development
Automatic creation layouts and viewport. Batch printing drawings from model.
www.kdmsoft.net
0 Likes
Message 8 of 29

angelinabrown0123
Advocate
Advocate

@maratovich 

But I am looking for VBA code, if I need software in future I will contact you.

0 Likes
Message 9 of 29

norman.yuan
Mentor
Mentor

@angelinabrown0123 ,

I heard your call for a code sample, but did not have time due to busy work until now. I wrote sample VBA code that works exactly as you expected, hopefully. Just for better readability, I post the code in my blog instead of here:

 

https://drive-cad-with-code.blogspot.com/2021/11/use-com-apivba-to-open-viewport-on.html 

 

HTH

 

Norman Yuan

Drive CAD With Code

EESignature

Message 10 of 29

angelinabrown0123
Advocate
Advocate

@norman.yuan 

I saw the video and the code, I really liked it a lot but I'm having a little trouble calling it, although I don't know much VBA, can you please provide the calling code as well.

Thank you

0 Likes
Message 11 of 29

grobnik
Collaborator
Collaborator

@angelinabrown0123 

You have two code versions, you have just look at link.

On 18th November I shared a link from EdJobe see:

https://forums.autodesk.com/t5/vba/get-the-coordinates-of-a-viewport/m-p/10731081#M105655

 

And yesterday Norman shared the link where code and video has been shared see

 

Drive AutoCAD with Code: Use COM API/VBA to Open Viewport on Layout to Show Given Content in ModelSp...

 

I didn't tested both but for sure both will works fine for your application I guess.

If you will have some difficulties let us know in some way you will have the forum support.

Bye

0 Likes
Message 12 of 29

angelinabrown0123
Advocate
Advocate

@grobnik 

The first link code you provided places the viewport's frame into the model. and the second link code that @norman.yuan gave is correct but this code is divide into small functions that I don't fully understand, you know how to trigger it so you can make me "SUB()" that able to run the code.

@norman.yuan 

Or I request @norman.yuan to give me the complete code as shown in the video.

 

I hope you will give me the code.

Thank in advanced

0 Likes
Message 13 of 29

norman.yuan
Mentor
Mentor

@angelinabrown0123 

I am sorry that I do not fully understand your request: my code posted in my blog is complete code. It is meant to show how to get the job of opening a viewport on layer based on a specified area in modelspace. But it is not specifically coded to work against your drawing attached in your original post. If you do have basic AutoCAD VBA knowledge, you can adapt the code with just minor changes for your use. To run the VBA code I wrote, you copy the code into a VBA project's module/class and off you go, as simple as that.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 14 of 29

angelinabrown0123
Advocate
Advocate

@norman.yuan 

You have called "Sub OpenPVport()" in the video, also took a screenshot of the video as shown in the image below.

Video ScreenshotVideo Screenshot

 

And the "OpenPVport()" code in the second image is not visible anywhere on your site.

 

OpenPVport.png

0 Likes
Message 15 of 29

norman.yuan
Mentor
Mentor
Accepted solution

Oops, I am terribly sorry that I posted duplicated class "BorderBlock" and missed the macro "OpenPVport". Thank you @angelinabrown0123 pointed it out.

 

The the code in my article has been updated.

 

Norman Yuan

Drive CAD With Code

EESignature

Message 16 of 29

grobnik
Collaborator
Collaborator

@angelinabrown0123 

I guess this can help you more see attached zip containing dvb file project already separated the class definition section and VBA section.

As second issue the Viewport border on ModelSpace shall be a block called as "VPORT_BOUNDARY", if not you have to change the block name into the proceudere.

And the active layout name is defined into procedure as "Layout1" if not you have to change the name into procedure.

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

 I tested and seems to work even if I have some other issue with the scale, I'll investigate more, in case I'll ask to @norman.yuan 

Message 17 of 29

grobnik
Collaborator
Collaborator

@norman.yuan 

Hi Norman, I would like to thank you very much for useful code. It's working perfectly.

If you can help us a litte bit more:

I have some issue with centering the ModelSpace border selected into viewport on Layout. Both has the same size A3.

I saw a section in the code concerning the position (angle, corner, etc.), but I have some difficulties to understand.

I already have a drawing title block the same on several layout, a company form A3 size with logo, and i have some issue with centering the bored block with drawing title block. Could you spent some additional time to help me fixing this issue ?.

I already fixed the scale setting to 1 instead 0,05 as your code indicated I'll prepare an example and I'll share with you for better understanding.

 

Thank you.

 

0 Likes
Message 18 of 29

angelinabrown0123
Advocate
Advocate

@norman.yuan 

Now it's perfect, actually you made great code, it will be very helpful for me, @norman.yuan  thank you very much for giving the code and updating it🙂.
And many thanks to everyone involved in this forum, I'll try it for the rectangle as well.

 

Bye

0 Likes
Message 19 of 29

angelinabrown0123
Advocate
Advocate

@norman.yuan 

I tested your code with attached drawing of Post No.1 it generates viewport but does not take selected block in center you can check in gif.VideoshootVideoshoot

0 Likes
Message 20 of 29

norman.yuan
Mentor
Mentor

It is difficult to say without having the drawing to play and without knowing what code change you have made (or not made). It seems to me, though, the corners' coordinates used for ZoomWindow() call may not be correct. Have you tried to verify the coordinates of the corners in debugging?

 

You can add following 2 lines of code right before ZoomWindow [llcorner], [urcorner]:

 

debug.Print border.LLCorner(0) & ", " & borderLLCorner(1)

debug.Print border.URCorner(0) & ", " & borderURCorner(1)

 

Seeing what the coordinates are, then go to ModelSpace to verify the corner positions of the selected border block.

Norman Yuan

Drive CAD With Code

EESignature