Jump to content

MSPACE won't take new viewport


ElAmigo

Recommended Posts

Hello friends,

 

I have the following piece of code, which rescales parts in de modelspace and after it has done this makes a corresponding viewport. This is an Inventor drawing exported to DWG. So I have a huge "drawing" in the modelspace which I am editing to certain specifications with LISP.

The problem is, I already have a paperspace viewport which takes the whole drawing, after which I rescale a part of the "drawing" (because that part of the drawing had a different scale than the drawing) to make it 1:1 in modelspace. After making the corresponding viewport I use MSPACE to scale and zoom it. The command: changeallviewportslayer is to lock all viewports and put them in the layer called VPORTS. So the first viewport is already in layer VPORTS and this new one gets made in layer 0.

When I use all my functions seperately the code I have below this text works, it takes the right viewport with MSPACE and does everything beautifully. But when I put this code in the whole program it always takes the wrong viewport. I have spent days trying to fix this but I can't seem to find an answer to this. Because selecting the viewport and using MSPACE still takes the wrong viewport.

The code works as a standalone command, so all variables and functions are determined and work correctly.

 

I really hope someone can help with this.

 

(defun c:Rescaler (/ factor p1 p2 basePoint newPoint displacementVector answer ent i data zsch2 newScale newFactorFix PVPP1 PVPP2 selection)
  (setq newFactor (getreal "\nEnter new scale (e.g., 1:10 = 10): "))                                         ;;Enter new scale
  (setq factor (/ (float newFactor) oldFactor))                                                              ;;Calculate magnification factor
  (DetermineDimensionStyle2)                                                                                 ;;Determine which dimension style selected 																												dimensions will get
  (setq p1 (getpoint "\nSelect the first point of the box: "))                                               ;;Ask the user for two points defining the 																												box
  (setq p2 (getcorner p1 "\nSelect the diagonally opposite point of the box: "))
  (setq PVPP1 (list (/ (car p1) oldFactor) (/ (cadr p1) oldFactor)))
  (setq PVPP2 (list (/ (car p2) oldFactor) (/ (cadr p2) oldFactor)))
  (setq selection (ssget "W" p1 p2))                                                                         ;;Select all objects within the specified 																													box
  (setq basePoint (list (/ (+ (car p1) (car p2)) 2) (/ (+ (cadr p1) (cadr p2)) 2)))                          ;;Determine the base point for scaling (the 																												center of the box)
  (if selection                                                                                              ;;If objects are selected, then scale them
    (progn
      (command "_scale" selection "" basePoint factor)                                                       ;;Scale all selected objects
      (setq newPoint (getpoint "\nSelect a new point to move the objects to: "))                             ;;Ask the user for a new point for moving
      (setq displacementVector (list (- (car newPoint) (car basePoint)) (- (cadr newPoint) (cadr basePoint)))) ;;Calculate the displacement vector
      (command "_move" selection "" "" displacementVector)                                                   ;;Move the selected objects
      (princ (strcat "\nObjects scaled by a factor of: " (rtos factor) " and moved."))
      (setq i 0)
      (setq newFactorFix (fix newFactor))
      (setq newScale (strcat "1:" (itoa newFactorFix)))
      (setq zsch2 (strcat "1/" (itoa newFactorFix) "XP"))
      (command "_.-DIMSTYLE" "_R" DimStyleOverride2 "") ; Set the current dimension style to DimStyleOverride2
      (command "_-DIMSTYLE" "_A" selection "")
      (command "CTAB" Document_ID)
      (command "MVIEW" PVPP1 PVPP2 "_MSPACE")
      (command "_CANNOSCALE" newScale "" "")
      (command "ZOOM" "S" zsch2 "")
      (command "ZOOM" "C" newPoint "" "")
      (command "_PSPACE")

      (c:ChangeAllViewportsLayer)
    )
  )  
  (if (> i 0) (princ (strcat "\nDimension styles adjusted to " DimStyleOverride2 ".")))
  (setq answer (getstring "\nIs more scaling needed? (Y/N): "))                                             ;;Ask user if more scaling is needed
    (if (= (strcase answer) "N")
     (progn
       (princ "\nScaling ended.")
     )
      (if (= (strcase answer) "Y")
        (progn
          (command "CTAB" "Model")
          (c:Rescaler)
        )
        (princ "\nInvalid input. Only Y or N is allowed.")
      )
    )
  (princ)
)

 

Link to comment
Share on other sites

Just a quick look can you also share DetermineDimensionStyle2 LISP and does this return any variable?

Have you localised all the variables - that often fixes things where they go odd

Link to comment
Share on other sites

6 minutes ago, Steven P said:

Just a quick look can you also share DetermineDimensionStyle2 LISP and does this return any variable?

Have you localised all the variables - that often fixes things where they go odd

All variables that are local have been localised. Some should be global so those ofcourse aren't and I've made sure not to reuse these variables.

 

(defun determineDimensionStyle2 ()
  (setq DimStyleOverride2
    (cond
      ((= newFactor 2) "Change2")
      ((= newFactor 5) "Change5")
      ((= newFactor 10) "Change10")
      ((= newFactor 15) "Change15")
      ((= newFactor 20) "Change20")
      ((= newFactor 25) "Change25")
      ((= newFactor 30) "Change30")
      ((= newFactor 40) "Change40")
      ((= newFactor 50) "Change50")
      ((= newFactor 100) "Change100")
      ((= newFactor 200) "Change200")
      ((= newFactor 250) "Change250")
      ((= newFactor 300) "Change300")
      ((= newFactor 400) "Change400")
      ((= newFactor 500) "Change500")
      ((= newFactor 1000) "Change1000")      
    )
  )
)

This is the function you also asked to see. These styles are all a company name with a number corresponding to scale, so I just named them "Change" instead of the company name. This is just to set the dimension style of the rescaled objects.

Link to comment
Share on other sites

Without code you can use "ZOOM 100XP" to zoom in at a point scale 1:100 etc. Say use ZOOM C PT scale 1st the scale is not critical what it is doing is centralising your viewport.

 

I would maybe work other way around, if you have fixed Title sheets in a layout with a mview set can use Layout Copy then go to new layout then mspace and set the scales.

 

Ok maybe the problem if you have say 4 mviews in one layout you can set which viewport you want to set, only thing hidden is that number of viewports is always plus 1, ie Paperspace is counted as a viewport.

 

We had fixed title blocks and they were always at 1=1 true size in a layout, when I see this see problems.

 

(setq p1 (getpoint "\nSelect the first point of the box: "))                                               ;;Ask the user for two points defining the 																												box
  (setq p2 (getcorner p1 "\nSelect the diagonally opposite point of the box: "))

Look at this all the matching numbers are in the code.

image.png.82ed84f489ae2516903398d8d37e2c95.png

 

You can have a layout with multiple mviews preset. These don't even have to exist in current dwg can be in say a master layout dwg, Layout Copy Template dwgname layoutname

image.png.319f2f716921ec36d5b97dd2f8050484.png

 

 

Link to comment
Share on other sites

I still haven't been able to solve this issue. The problem occurs with AutoCAD not recognizing the new viewport during the code, and when the code crashes it can recognize it. I found this out using the command "CVPORT". After creating the new viewport, this viewport is number 3. When I add to my code:

  (command "_MSPACE")
  (command "CVPORT" "3")

I get:

Enter new value for CVPORT <2>: 3
Cannot set CVPORT to that value.

After this the code chrashes. After this crash I use these EXACT SAME commands and it works fine... I am at a loss why this viewport is somehow not recognized during my code running.

Link to comment
Share on other sites

For some reason the variable coordinates make it not function properly:

      (command "_.MVIEW" PVPP1 PVPP2)

I did a test when I made a new viewport after this one using some random coordinates:

      (command "_.MVIEW" PVPP1 PVPP2)
      (command "_.MVIEW" "230,100" "380,180")  
      (command "_MSPACE")

After the MSPACE command it actually goes into the fourth viewport (the one with the randomly chosen coordinates). THEN when I use CVPORT it actually recognizes that viewport as number 4, but I still cannot set it to 3...

Link to comment
Share on other sites

The viewports are numbered as 1=Pspace 2=Viewport, 3= viewport and so on. Note a number not "2".

 

(command "CVPORT" 2)

 

Link to comment
Share on other sites

On 3/20/2024 at 11:39 PM, BIGAL said:

The viewports are numbered as 1=Pspace 2=Viewport, 3= viewport and so on. Note a number not "2".

 

(command "CVPORT" 2)

 

Even with CVPORT it wouldn't work. I did find a rather stupid but working solution. To put a ZOOM inbetween. I tried using a REGEN which was useless. But for some reason the ZOOM does sort of 'regenerate' the sheet which makes it recognize the viewport. I solved it like this:

 

      (command "CTAB" Document_ID) ;;Document_ID is the layout name
      (command "_.MVIEW" PVPP1 PVPP2) ;;creates the new viewport in the desired location
      (command "ZOOM" "E")			;;useless zoom to sort of regenerate the sheet
      (command "_MSPACE")			;;Go into one of the viewports
      (command "CVPORT" (itoa *viewporCounter*)) ;;viewportCounter is a new variable to go into the right viewport

 

Link to comment
Share on other sites

Not sure about a couple of things this is my approach. Dont forget paperspace is a viewport.

 

(setvar 'CTAB Document_ID) ;;Document_ID is the layout name
(command "_.MVIEW" PVPP1 PVPP2) ;;creates the new viewport in the desired location
(setq *viewporCounter* (sslength (ssget "X" '((0 . "viewport")(cons 410 (getvar 'ctab)))))
(command "_MSPACE")			;;Goes into last viewport made
(command "ZOOM" "E")		;;useless zoom to sort of regenerate the sheet

 

Link to comment
Share on other sites

On 3/23/2024 at 12:02 AM, BIGAL said:

Not sure about a couple of things this is my approach. Dont forget paperspace is a viewport.

 

(setvar 'CTAB Document_ID) ;;Document_ID is the layout name
(command "_.MVIEW" PVPP1 PVPP2) ;;creates the new viewport in the desired location
(setq *viewporCounter* (sslength (ssget "X" '((0 . "viewport")(cons 410 (getvar 'ctab)))))
(command "_MSPACE")			;;Goes into last viewport made
(command "ZOOM" "E")		;;useless zoom to sort of regenerate the sheet

 

Yeah I set viewportcounter to 2, when I make my first viewport it is viewport 3 so I set the counter +1 everytime I make a new viewport. Then I use CVPORT viewportcounter to go into the right viewport and set the scale and the correct zoom etc. Unless I use "ZOOM" "E" it won't recognize the newest viewport I make without crashing my code. The way I did it works every time now.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...