Jump to content

Trim in lisp removes to much


Ament

Recommended Posts

Hello together,

 

I'm kind of sure that some one else has the same issue, but after searching this forum for one hour I couldn't find a solution for my issue:

 

In lisp I'm selectiong 2 points to define a frame, with that frame I want to trim all elements passing this frame. Afterwards I delete all objects inside the frame which didn't exceed it and the frame itself.

 

However if a line crosses the frame two times not only the surround part, but sometimes also the remaining lenght outside of the frame is deleted.

 

While doing it line by line it seems like the frame is also trimmed so that only one edge is remaining, but in my understanding I should not have this elements in my list of objects for trim

 

 

Does someone has an idea how I could avoid this issue?

 

 

 

I'll copy my code below:

 

(initget "Yes No")
       (setq ans (getkword "\nDo you want to delete objects obviously not to be imported? [Yes/No]?: "))
       (while (= ans "Yes")                                                                    ;While user decide YES
           (progn                                                                                 ;Commands to run
               (prompt "\nSelect objects by window")                                            ;Tells user what to do
               (setq P1 (getpoint "\nFirst Corner: "))                                            ;Pick first corner
               (setq P2 (getpoint "\nSecond Corner: "))                                        ;Pick second corner
               (setq P3 (list (nth 0 P2)(nth 1 P1)))                                            ;Create additional point for frame
               (setq P4 (list (nth 0 P1)(nth 1 P2)))                                            ;Create additional point for frame
               (setq P5 (list (/ (+ (nth 0 P1)(nth 0 P2)) 2)(/ (+ (nth 1 P1)(nth 1 P2)) 2)))    ;Select point in middle of frame
               (command "_Pline" P1 P3 P2 P4 "_c")                                                ;Create frame
               (setq tmp (entlast))                                                            ;Select created frame
               (setq P6 (list (- (nth 0 P1)0.1)(- (nth 1 P1)0.1)))                                ;define points inside of the frame as selection for trim
               (setq P7 (list (- (nth 0 P2)0.1)(- (nth 1 P2)0.1)))                                ;define points inside of the frame as selection for trim
               (setq tbe (ssget "_C" P6 P7))                                                    ;Select objects touching the frame (tbe)
               (command "_Trim" tmp "")                                                        ;Open trim command
               (setq ct -1)                                                                    ;reset counter
               (repeat (sslength tbe)                                                            ;run through all objects in tbe selection
                   (command(list(ssname tbe (setq ct (1+ ct))) P5))                            ;Fill required prompts for trim command
               )                                                                                ;End of repeat
               (command "")                                                                    ;Leave trim function
               (command "._Erase" tmp "")                                                        ;Remove Frame
               (princ)                                                                            ;
               (setq ct -1)                                                                    ;Reset counter
               (setq tbe nil)                                                                    ;Reset list tbe
               (setq tbe (ssget "_W" P1 P2))                                                    ;Select all objects in the Window opened by P1 and P2
               (command "._Erase" tbe "")                                                        ;Delete former selected objects
               (setq tbe nil)                                                                    ;Reset list tbe
               (vla-zoomextents (vlax-get-acad-object))                                        ;Zoom Extents
               (initget "Yes No")                                                                ;Allow user input
               (setq ans (getkword "\nDo you want to delete more? [Yes/No]?: "))                ;Ask user if more should be deleted
               (if (= ans "No")                                                                ;Start of if, cond. Answer of former question was no
               (setq ans nil)                                                                    ;Set var. ans to nil 
               (princ)
               )                                                                                ;end of if
           )                                                                                    ;End of progn
       )                                                                                        ;End of while
       (princ)
       (setq ct -1)    

Link to comment
Share on other sites

  • Replies 21
  • Created
  • Last Reply

Top Posters In This Topic

  • Ament

    11

  • dlanorh

    7

  • BIGAL

    4

Hello together,

 

I'm kind of sure that some one else has the same issue, but after searching this forum for one hour I couldn't find a solution for my issue:

 

In lisp I'm selectiong 2 points to define a frame, with that frame I want to trim all elements passing this frame. Afterwards I delete all objects inside the frame which didn't exceed it and the frame itself.

 

However if a line crosses the frame two times not only the surround part, but sometimes also the remaining lenght outside of the frame is deleted.

 

While doing it line by line it seems like the frame is also trimmed so that only one edge is remaining, but in my understanding I should not have this elements in my list of objects for trim

 

 

Does someone has an idea how I could avoid this issue?

 

 

 

I'll copy my code below:

 

(initget "Yes No")
       (setq ans (getkword "\nDo you want to delete objects obviously not to be imported? [Yes/No]?: "))
       (while (= ans "Yes")                                                                    ;While user decide YES
           (progn                                                                                 ;Commands to run
               (prompt "\nSelect objects by window")                                            ;Tells user what to do
               (setq P1 (getpoint "\nFirst Corner: "))                                            ;Pick first corner
               (setq P2 (getpoint "\nSecond Corner: "))                                        ;Pick second corner
               (setq P3 (list (nth 0 P2)(nth 1 P1)))                                            ;Create additional point for frame
               (setq P4 (list (nth 0 P1)(nth 1 P2)))                                            ;Create additional point for frame
               (setq P5 (list (/ (+ (nth 0 P1)(nth 0 P2)) 2)(/ (+ (nth 1 P1)(nth 1 P2)) 2)))    ;Select point in middle of frame
               (command "_Pline" P1 P3 P2 P4 "_c")                                                ;Create frame
               (setq tmp (entlast))                                                            ;Select created frame
               (setq P6 (list (- (nth 0 P1)0.1)(- (nth 1 P1)0.1)))                                ;define points inside of the frame as selection for trim
               (setq P7 (list (- (nth 0 P2)0.1)(- (nth 1 P2)0.1)))                                ;define points inside of the frame as selection for trim
               (setq tbe (ssget "_C" P6 P7))                                                    ;Select objects touching the frame (tbe)
               (command "_Trim" tmp "")                                                        ;Open trim command
               (setq ct -1)                                                                    ;reset counter
               (repeat (sslength tbe)                                                            ;run through all objects in tbe selection
                   (command(list(ssname tbe (setq ct (1+ ct))) P5))                            ;Fill required prompts for trim command
               )                                                                                ;End of repeat
               (command "")                                                                    ;Leave trim function
               (command "._Erase" tmp "")                                                        ;Remove Frame
               (princ)                                                                            ;
               (setq ct -1)                                                                    ;Reset counter
               (setq tbe nil)                                                                    ;Reset list tbe
               (setq tbe (ssget "_W" P1 P2))                                                    ;Select all objects in the Window opened by P1 and P2
               (command "._Erase" tbe "")                                                        ;Delete former selected objects
               (setq tbe nil)                                                                    ;Reset list tbe
               (vla-zoomextents (vlax-get-acad-object))                                        ;Zoom Extents
               (initget "Yes No")                                                                ;Allow user input
               (setq ans (getkword "\nDo you want to delete more? [Yes/No]?: "))                ;Ask user if more should be deleted
               (if (= ans "No")                                                                ;Start of if, cond. Answer of former question was no
               (setq ans nil)                                                                    ;Set var. ans to nil 
               (princ)
               )                                                                                ;end of if
           )                                                                                    ;End of progn
       )                                                                                        ;End of while
       (princ)
       (setq ct -1)    

 

 

 

 

(setq P6 (list (- (nth 0 P1)0.1)(- (nth 1 P1)0.1)))                                 ;define points inside of the frame as selection for trim
(setq P7 (list (- (nth 0 P2)0.1)(- (nth 1 P2)0.1)))                                 ;define points inside of the frame as  selection for trim

This code is also selecting the pline and some objects outside it. If P6 is the Bottom Left corner it should be

 

(setq P6 (list (+ (nth 0 P1)0.1)(+ (nth 1 P1)0.1)))                                  ;define points inside of the frame as selection for trim

You should ask for the Bottom Left Corner and Top Right Corner, or compare coords to check which is the Bottom Left. The bottom left then will always be +

Edited by dlanorh
readability
Link to comment
Share on other sites

Hm, I would be happy if that solved the issue.

 

 

 

I modified my lisp now looking like this:

 

(initget "Yes No")
       (setq ans (getkword "\nDo you want to delete objects obviously not to be imported? [Yes/No]?: "))
       (while (= ans "Yes")                                                                    ;While user decide YES
           (progn                                                                                 ;Commands to run
               (prompt "\nSelect objects by window")                                            ;Tells user what to do
               (setq P1 (getpoint "\nFirst Corner: "))                                            ;Pick first corner
               (setq P2 (getpoint "\nSecond Corner: "))                                        ;Pick second corner
               (setq P3 (list (nth 0 P2)(nth 1 P1)))                                            ;Create additional point for frame
               (setq P4 (list (nth 0 P1)(nth 1 P2)))                                            ;Create additional point for frame
               (setq P5 (list (/ (+ (nth 0 P1)(nth 0 P2)) 2)(/ (+ (nth 1 P1)(nth 1 P2)) 2)))    ;Select point in middle of frame
               (command "_Pline" P1 P3 P2 P4 "_c")                                                ;Create frame
               (setq tmp (entlast))                                                            ;Select created frame
               
                                                                                               ;Identify point positions left/right & up/dn
               (setq xp1 (nth 0 P1))                                                            ;set xp1 as x coord of P1
               (setq xp2 (nth 0 P2))                                                            ;set xp2 as x coord of P2
               (setq yp1 (nth 1 P1))                                                            ;set yp1 as y coord of P1
               (setq yp2 (nth 1 P2))                                                            ;set yp2 as y coord of P2
               (if (< xp1 xp2)                                                                    ;check x relative and set values
                   (progn
                       (setq xp6 (+ xp1 1))
                       (setq xp7 (- xp2 1))
                   )
                   (progn
                       (setq xp6 (- xp1 1))
                       (setq xp7 (+ xp2 1))
                   )
               )
               (if (< yp1 yp2)                                                                    ;check y relative and set values
                   (progn
                       (setq yp6 (+ yp1 1))
                       (setq yp7 (- yp2 1))
                   )
                   (progn
                       (setq yp6 (- yp1 1))
                       (setq yp7 (+ yp2 1))
                   )
               )    
               (setq P6 (list xp6 yp6))                                ;define points inside of the frame as selection for trim
               (setq P7 (list xp7 yp7))                                ;define points inside of the frame as selection for trim
               (setq tbe (ssget "_C" P6 P7))                                                    ;Select objects touching the frame (tbe)
               (command "_Trim" tmp "")                                                        ;Open trim command
               (setq ct -1)                                                                    ;reset counter
               (repeat (sslength tbe)                                                            ;run through all objects in tbe selection
                   (command(list(ssname tbe (setq ct (1+ ct))) P5))                            ;Fill required prompts for trim command
               )                                                                                ;End of repeat
               (command "")                                                                    ;Leave trim function
               (command "._Erase" tmp "")                                                        ;Remove Frame
               (princ)                                                                            ;
               (setq ct -1)                                                                    ;Reset counter
               (setq tbe nil)                                                                    ;Reset list tbe
               (setq tbe (ssget "_W" P1 P2))                                                    ;Select all objects in the Window opened by P1 and P2
               (command "._Erase" tbe "")                                                        ;Delete former selected objects
               (setq tbe nil)                                                                    ;Reset list tbe
               (vla-zoomextents (vlax-get-acad-object))                                        ;Zoom Extents
               (initget "Yes No")                                                                ;Allow user input
               (setq ans (getkword "\nDo you want to delete more? [Yes/No]?: "))                ;Ask user if more should be deleted
               (if (= ans "No")                                                                ;Start of if, cond. Answer of former question was no
               (setq ans nil)                                                                    ;Set var. ans to nil 
               (princ)
               )                                                                                ;end of if
           )                                                                                    ;End of progn
       )                                                                                        ;End of while
       (princ)
       (setq ct -1)    

 

 

Unfortunately it didn't solve the issue. After some more testing it looks like it is working well if i have the centre point of a line enclosed with the frame, but as soon as it only cuts an opening into the line close to an extent without enclosing the middlepoint it removes the whole remaining part including the part outside of the frame :(

Link to comment
Share on other sites

A variation on the theme you can offset the outside pline inwards a small amount and use entlast when selecting trim cutting object, this will trim all and then as a 2nd step use the WP with an erase to get any objects left behind.

Link to comment
Share on other sites

Of course, an offset would have helped to. Didn't think about. But with the Points P6 and P7 I'm doing pretty much the same. If it just would make a break into the line and doesn't trim the whole end of it :(

Link to comment
Share on other sites

If you make the cutter the original pline then use the offset pline as a "fence" option using points, it will trim right back to the original pline with no little bits left behind. You would then use the inner pline in conjunction with a "within poly" option to erase anything left.

Link to comment
Share on other sites

Hm, I would be happy if that solved the issue.

 

 

 

I modified my lisp now looking like this:

 

(initget "Yes No")
       (setq ans (getkword "\nDo you want to delete objects obviously not to be imported? [Yes/No]?: "))
       (while (= ans "Yes")                                                                    ;While user decide YES
           (progn                                                                                 ;Commands to run
               (prompt "\nSelect objects by window")                                            ;Tells user what to do
               (setq P1 (getpoint "\nFirst Corner: "))                                            ;Pick first corner
               (setq P2 (getpoint "\nSecond Corner: "))                                        ;Pick second corner
               (setq P3 (list (nth 0 P2)(nth 1 P1)))                                            ;Create additional point for frame
               (setq P4 (list (nth 0 P1)(nth 1 P2)))                                            ;Create additional point for frame
               (setq P5 (list (/ (+ (nth 0 P1)(nth 0 P2)) 2)(/ (+ (nth 1 P1)(nth 1 P2)) 2)))    ;Select point in middle of frame
               (command "_Pline" P1 P3 P2 P4 "_c")                                                ;Create frame
               (setq tmp (entlast))                                                            ;Select created frame
               
                                                                                               ;Identify point positions left/right & up/dn
               (setq xp1 (nth 0 P1))                                                            ;set xp1 as x coord of P1
               (setq xp2 (nth 0 P2))                                                            ;set xp2 as x coord of P2
               (setq yp1 (nth 1 P1))                                                            ;set yp1 as y coord of P1
               (setq yp2 (nth 1 P2))                                                            ;set yp2 as y coord of P2
               (if (< xp1 xp2)                                                                    ;check x relative and set values
                   (progn
                       (setq xp6 (+ xp1 1))
                       (setq xp7 (- xp2 1))
                   )
                   (progn
                       (setq xp6 (- xp1 1))
                       (setq xp7 (+ xp2 1))
                   )
               )
               (if (< yp1 yp2)                                                                    ;check y relative and set values
                   (progn
                       (setq yp6 (+ yp1 1))
                       (setq yp7 (- yp2 1))
                   )
                   (progn
                       (setq yp6 (- yp1 1))
                       (setq yp7 (+ yp2 1))
                   )
               )    
               (setq P6 (list xp6 yp6))                                ;define points inside of the frame as selection for trim
               (setq P7 (list xp7 yp7))                                ;define points inside of the frame as selection for trim
               (setq tbe (ssget "_C" P6 P7))                                                    ;Select objects touching the frame (tbe)
               (command "_Trim" tmp "")                                                        ;Open trim command
               (setq ct -1)                                                                    ;reset counter
               (repeat (sslength tbe)                                                            ;run through all objects in tbe selection
                   (command(list(ssname tbe (setq ct (1+ ct))) P5))                            ;Fill required prompts for trim command
               )                                                                                ;End of repeat
               (command "")                                                                    ;Leave trim function
               (command "._Erase" tmp "")                                                        ;Remove Frame
               (princ)                                                                            ;
               (setq ct -1)                                                                    ;Reset counter
               (setq tbe nil)                                                                    ;Reset list tbe
               (setq tbe (ssget "_W" P1 P2))                                                    ;Select all objects in the Window opened by P1 and P2
               (command "._Erase" tbe "")                                                        ;Delete former selected objects
               (setq tbe nil)                                                                    ;Reset list tbe
               (vla-zoomextents (vlax-get-acad-object))                                        ;Zoom Extents
               (initget "Yes No")                                                                ;Allow user input
               (setq ans (getkword "\nDo you want to delete more? [Yes/No]?: "))                ;Ask user if more should be deleted
               (if (= ans "No")                                                                ;Start of if, cond. Answer of former question was no
               (setq ans nil)                                                                    ;Set var. ans to nil 
               (princ)
               )                                                                                ;end of if
           )                                                                                    ;End of progn
       )                                                                                        ;End of while
       (princ)
       (setq ct -1)    

Unfortunately it didn't solve the issue. After some more testing it looks like it is working well if i have the centre point of a line enclosed with the frame, but as soon as it only cuts an opening into the line close to an extent without enclosing the middlepoint it removes the whole remaining part including the part outside of the frame :(

 

 

I'm not certain, but I think part of the trim command needs to be in a while loop.

 

 

(setq ct -1)                                ;reset counter
(command "_Trim" tmp "")              ;Open trim command
 (while (= (getvar 'cmdactive) 1 )
   (repeat (sslength tbe)
     (command (list(ssname tbe (setq ct (1+ ct))) P5))
   ) 
   (command "")  ;Leave trim function
 );end_while

Link to comment
Share on other sites

I tried to run it in the loop, but nothing changedd. It's so frustrating. It seems like it is trimming the lines only at one edge of the box and not between both limits.. :(

Link to comment
Share on other sites

Hi everyone, thanks for your help. Expecially to dlanorth for pointing out my offes mistake.

 

Is someone has an idea what could solve the issue that my lines get not broken on both sides before they are removed please leave me a hint. The initial issue isn't solved unfortunately.

Link to comment
Share on other sites

Ament,

 

I found some code that i used to trim out lines between two other lines and have adapted it to suit your need. Most of your code remains intact, but I've replaced creating the frame with the rectangle command.

 

I've also changed the program flow slightly. It first deletes any object wholly within the frame to avoid any trim errors. Then trims the objects crossing the frame. This has been changed so that it loops through the selection set and trims each entity seperately. I think the problem was here somewhere. It might take a fraction longer, but better than doing it by hand.

 

After that it again searches for any objects within the frames left behind by the trim, and erases them.

 

I've put this into a function to test. It works for me in 2012.

You should be able to slot this back into your original function

 

(defun c:ttest ( / )
              (setq P1 (getpoint "\nFirst Corner: "))                                    ;Pick first corner
               (setq P2 (getpoint "\nSecond Corner: "))                                  ;Pick second corner
               (setq P3 (list (/ (+ (car p1) (car p2)) 2) (/ (+ (cadr p1) (cadr p2)) 2)));THIS IS MIDDLE OF RECTANGLE
               (setq ct -1)
               (setq tbe (ssget "_W" P1 P2))                                             ;Select all objects in the Window opened by P1 and P2
               (repeat (sslength tbe)                                                    ;run through all objects in tbe selection
                 (setq ent (ssname tbe (setq ct (1+ ct))))                               
                 (vl-cmdf "._Erase" ent "")                                              ;DELETE EVERY ENTITY THAT WON'T BE TRIMMED TO AVOID TRIM ERROR 
               );End of repeat
               (setq tbe nil)
               (command "_rectangle" P1 P2)                                              ;Create frame
               (setq tmp (entlast))                                                      ;Select created frame                                                                                                               ;Identify point positions left/right & up/dn
               (setq xp1 (nth 0 P1))                                                     ;set xp1 as x coord of P1
               (setq xp2 (nth 0 P2))                                                     ;set xp2 as x coord of P2
               (setq yp1 (nth 1 P1))                                                     ;set yp1 as y coord of P1
               (setq yp2 (nth 1 P2))                                                     ;set yp2 as y coord of P2
               (if (< xp1 xp2)                                                           ;check x relative and set values
                   (progn
                       (setq xp6 (+ xp1 1.0))
                       (setq xp7 (- xp2 1.0))
                   )
                   (progn
                       (setq xp6 (- xp1 1.0))
                       (setq xp7 (+ xp2 1.0))
                   )
               )
               (if (< yp1 yp2)                                                           ;check y relative and set values
                   (progn
                       (setq yp6 (+ yp1 1.0))
                       (setq yp7 (- yp2 1.0))
                   )
                   (progn
                       (setq yp6 (- yp1 1.0))
                       (setq yp7 (+ yp2 1.0))
                   )
               )
               
               (setq P6 (list xp6 yp6))                                ;define points inside of the frame as selection for trim
               (setq P7 (list xp7 yp7))                                ;define points inside of the frame as selection for trim
               (setq tbe (ssget "_C" P6 P7))                           ;Select objects crossing the frame
               (setq ct -1)                                            ;reset counter
               (repeat (sslength tbe)                                  ;run through all objects in tbe selection
                 (setq ent (ssname tbe (setq ct (1+ ct)))              ;get name of entity
                       pp (vlax-curve-getClosestPointTo ent p3)        ;get closest point of entity to mid pt of frame
                 )
                 (vl-cmdf "_.trim" tmp "" (nentselp pp) "")            ;trim entity by selecting it using (nentselp) where pp is the closest point inside frame
               );End of repeat
               (command "._Erase" tmp "")                              ;erase Frame
               (setq ct -1)                                            ;Reset counter
               (setq tbe nil)                                          ;clear selection set
               (setq tbe (ssget "_W" P1 P2))                           ;Select any entities left behind by trim
               (repeat (sslength tbe)                                  ;run through all objects in tbe selection
                 (setq ent (ssname tbe (setq ct (1+ ct))))             ;get entity name
                 (vl-cmdf "._Erase" ent "")                            ;erase entity
               );End of repeat
               (setq tbe nil)                                          ;Reset list tbe
);end_defun  

Edited by dlanorh
spelling
Link to comment
Share on other sites

@dlanorth I'm trying to get your code flying, but I'm struggeling.

 

The issue is that i get " bad argument type: lselsetp nil" when the code is running. From my point of view it looks like I'm getting trouble with the "vl..." commands

If I check the variables after the error I can call back P3 but I dont get any values for "tbe" or "ent".

I read that it might be related to the rights of the user so I'm running AutoCAD as an administrator now, but it didn't help at all. Feels like I'm far from being able to walk on my own feets.. :(

Best regards,

Ament

Edited by Ament
Wrong conclusion on my side
Link to comment
Share on other sites

Oh.. I found it.. But first things first. The line (vl-load-com) didn't help my in my next tries, but afterwards I had the Idea..

 

The code didn't cover that there might be no objects completly inside the selection which erases inside objects. That produced the issue while testing it. ;) *facepalm*

 

I added a "if" clause to compensate the case.

 

 

 

Unfortunately it is still giving me same result as the code i wrote. Some edges are not split into two, but just cut at one of the edges and to much was removed.

 

What I found out while pressing ctrl+z to get back to the start after running the lisp is that actually the first ctrl-z brings back the "rectangle" used to cut. But one edge is missing.

 

Always the same, One of the rectengular edges seems to be trimmed first. Somehow the selection set includes the first rectangle. I highlighted the content of tbe after the (ssget "_C" P6 P7) and it was included.

 

I'll keep investigating and come up with whatever I find.

Link to comment
Share on other sites

As i said the code i posted works for me in 2012, although i made sure there was an object completely inside the frame when testing :facepalm:

 

It could be system variables. I have TRIMMODE =1 and EDGEMODE = 0

Link to comment
Share on other sites

Hm, It's working for in small scale like I'm cutting some 10mm hole inside some objects. But when my frame gets to big (like 4000mm) it removes one edge of the frame first. While writing this, this might be the issue.. Maybe I have an issue because the distance between both rectangles is to small in big scale. Actually if I make a rectangle between P6 and P7 it gets same size than between P1 and P2.. Is there some detection in AutoCAD which corrects this kind of small gaps to support user? (Or in my case to make my life hard ;))

Link to comment
Share on other sites

That's it.. I just added the code

 

(ssdel tmp tbe)

between selecting objects to trim and the actual trim command and everything is working well. I even don't need to calculate the Points P6 and P7 anymore. Even if the first rectangle is selected initially, I'm removing it anyway before trim :D

 

You can't believe how relieved I am right now. Time to start the weekend :D

 

Thank you for your help and all the hints!!

 

I'll copy the code below in case someone has the same issue:

 

(defun c:FDEL ( / )                                            ;Deletes everything inside and crossing the frame opened by two selected points
   (vl-load-com)
   (setq P1 (getpoint "\nFirst Corner: "))                                    ;Pick first corner
   (setq P2 (getpoint "\nSecond Corner: "))                                  ;Pick second corner
   (setq P3 (list (/ (+ (nth 0 P1)(nth 0 P2)) 2)(/ (+ (nth 1 P1)(nth 1 P2)) 2)));THIS IS MIDDLE OF RECTANGLE
   (setq ct -1)
   (setq tbe (ssget "_W" P1 P2))                                         ;Select all objects in the Window opened by P1 and P2
   (if (/= nil tbe)
       (progn
       (repeat (sslength tbe)                                            ;run through all objects in tbe selection
           (setq ent (ssname tbe (setq ct (1+ ct))))                               
           (vl-cmdf "._Erase" ent "")                                    ;DELETE EVERY ENTITY THAT WON'T BE TRIMMED TO AVOID TRIM ERROR 
   )))                                                                    ;End of repeat, progn and if
   (setq tbe nil)
   (command "_rectangle" P1 P2)                            ;Create frame
   (setq tmp (entlast))                                    ;Select created frame
   (setq tbe (ssget "_C" P1 P2))                           ;Select objects crossing the frame
   (ssdel tmp tbe)                                            ;removes rectangle from selection
   (setq ct -1)                                            ;reset counter
   (repeat (sslength tbe)                                  ;run through all objects in tbe selection
       (setq ent (ssname tbe (setq ct (1+ ct))))           ;get name of entity
       (setq pp (vlax-curve-getClosestPointTo ent p3))     ;get closest point of entity to mid pt of frame
       (vl-cmdf "_.trim" tmp "" (nentselp pp) "")          ;trim entity by selecting it using (nentselp) where pp is the closest point inside frame
   )                                                        ;End of repeat
   (command "._Erase" tmp "")                              ;erase Frame
   (setq ct -1)                                            ;Reset counter
   (setq tbe nil)                                          ;clear selection set
)                                                            ;end_defun

Link to comment
Share on other sites

  • 3 weeks later...

Hm, I thought it was done, but with my current drawing I'm strugeling..

 

Ill attach it below:

Drawing1.dwg

You can try i out by selecting two opposite corners of the white rectangle inside the drawing when the lisp (see below) asks for it. To start the lisp use MDEL as command after loading it.

 

My issue is that it still looks like that the code is trimming my rectangle first. (I can't find the issue in the lisp. I thought if I remove it from the selection set "tbe" there shouldn't be an issue) You can see it if you roll back the changes by the lisp using undo several times.

 

But additionally I got some new issue. It looks like some elements can't be trimmed (the red ones in the attached drawing) and I'm not sure why and how I could solve it so that the code doesn't run into an error. In current state I get the "ERROR: bad argument value: AcDbCurve 158" Is there a way to trim them? If not, is there a way to suppress the error and move on to next element so that the lisp is not canceled?

 

I'll put the last version of the lisp below:

 

(defun c:MDEL ( / )                                            ;Deletes everything inside the frame opened by two selected points
   (vl-load-com)
   (setq ans "Yes")
   (while (= ans "Yes")                                                                    ;While user decide YES
       (progn
           (setq P1 (getpoint "\nFirst Corner: "))                                    ;Pick first corner
           (setq P2 (getpoint "\nSecond Corner: "))                                  ;Pick second corner
           (setq P3 (list (/ (+ (nth 0 P1)(nth 0 P2)) 2)(/ (+ (nth 1 P1)(nth 1 P2)) 2)));THIS IS MIDDLE OF RECTANGLE
           (setq ct -1)
           (setq tbe (ssget "_W" P1 P2))                                         ;Select all objects in the Window opened by P1 and P2
           (if (/= nil tbe)
               (progn
               (command "._Erase" tbe "")
               ))                                                                    ;End of repeat, progn and if
           (setq tbe nil)
           (command "_rectangle" P1 P2)                            ;Create frame
           (setq tmp (entlast))                                    ;Select created frame
           (setq tbe (ssget "_C" P1 P2))                           ;Select objects crossing the frame
           (ssdel tmp tbe)                                            ;removes rectangle from selection
           (setq ct -1)                                            ;reset counter
           (repeat (sslength tbe)                                  ;run through all objects in tbe selection
               (setq ent (ssname tbe (setq ct (1+ ct))))           ;get name of entity
               (setq pp (vlax-curve-getClosestPointTo ent p3))     ;get closest point of entity to mid pt of frame
               (vl-cmdf "_.trim" tmp "" (nentselp pp) "")          ;trim entity by selecting it using (nentselp) where pp is the closest point inside frame
           )                                                        ;End of repeat
           (command "._Erase" tmp "")                              ;erase Frame
           (setq ct -1)                                            ;Reset counter
           (setq tbe nil)                                          ;clear selection set
           (vla-zoomextents (vlax-get-acad-object))                                        ;Zoom Extents
           (initget "Yes No")                                                                ;Allow user input
           (setq ans (getkword "\nDo you want to delete more? [Yes/No]?: "))                ;Ask user if more should be deleted
           (if (= ans "No")                                                                ;Start of if, cond. Answer of former question was no
               (setq ans nil)                                                                    ;Set var. ans to nil 
           (princ)
           )
       )
   )
)                                                            ;end_defun

Link to comment
Share on other sites

Sorry, I'm away for the week and only have the site laptop. Can you attach the drawing in Autocad 2007 format, and I'll take a look.

Link to comment
Share on other sites

Just a out there question are you zooming in and out to pick the points a known bug with code is if you are zoomed in often stuff like this trim does not work unless you can see the entire object.

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...