Author Topic: Function to trim block on polyline as well  (Read 1480 times)

0 Members and 1 Guest are viewing this topic.

trogg

  • Bull Frog
  • Posts: 255
Function to trim block on polyline as well
« on: March 26, 2013, 09:41:23 AM »
Hey guys,
Its been a while...

I'm wondering if someone could help me add the ability to trim polylines in the below function.

We have a lisp-based P&ID program that hasn't been updated in a while. The P&ID users use this a lot.
I can't post the entire routine, but i can send it in an email if someone thinks they can help.

The routine lets the users insert a block from a toolbar and it will align the block to the line and then trim the inner portion.
The lines are always horizontal or vertical and the users always draw with SNAP turned on.

Each block is called similar to what is shown below where the; 1) Layer is set 2) Breaking function is called 3) block name (dwg) is called and 4) the size of the break is defined:
Code: [Select]
;;;Inline Strainer w/ Gate Valve
(defun c:H3_INLINE_STRAINER_GATE()
(layerSymbol)
(blockInsertInLine "H3_inline_strainer_gate" 0.18750)
);end Inline Strainer w/ Gate Valve

And the function "BlockInsertInline" is shown below:
Code: [Select]
;;;******************************************************************************
;;;Function : blockInsertInLine                                                 *
;;;******************************************************************************
;;;Description : 1. Inserts symbol                                              *
;;;              2. Layer SYMBOL                                                *
;;;              3. Scale 1 to 1                                                *
;;;              4. Breaks line and auto orients symbol                         *
;;;                                                                             *
;;;******************************************************************************

(defun blockInsertInLine (blockName breakDistance)
;(defun blockInsertInLine ()
  (insertSettings)
  (insertScale)
  (osModeOff)
  (cleanFunctionStart)

  (setq symbolName (strcat SYMBOL_DIRECTORY blockName ".dwg"))
  (directoryCheck)

  (if (/= nil (findfile symbolName)) ;block was found on network
    (progn
      (getInsertionPoint)
      (setvar "osmode" 0)

      (if (= selectedEntity nil) ;selected point was not on a line
        (insertNoBreak) ;inserts without break
      ;else
        (progn
  (if (> breakDistance 0.0)
    (progn
      (setq ang nil)
      (breakLine)
    ) ;end progn
          ;else
    (setq ang nil)
  ) ;end if

  (if (= ang nil)
    (command ".insert" symbolName point1 scaleFactor scaleFactor pause)
  ;else
    (command ".insert" symbolName point1 scaleFactor scaleFactor ang)
  ) ;end if
        ) ;end progn
      ) ;end if
     
      (princ)
    ) ;end progn
  ) ;end if

;return object snap mode to existing state
  (setvar "osmode" existOsMode)

  (cleanFunctionEnd)
) ;end blockInsertInLine

;;;******************************************************************************
;;;Functions used by blockInsertInLine                                          *
;;;******************************************************************************
;;;getInsertionPoint: gets insertion point                                      *
;;;insertNoBreak: inserts symbol without breaking line                          *
;;;breakLine: determines angle of and breaks line                               *
;;;******************************************************************************
(defun getInsertionPoint ()
  (command ".insert" symbolName pause (command))
  (setq point1 (getvar "lastpoint"))
  (setq selectedEntity (ssget point1))
) ;end getInsertionPoint

(defun insertNoBreak () 
  (command ".insert" symbolName point1 "" "" pause)
) ;end insertNoBreak

; function to break line for symbol insertion
(defun breakLine ()
  (setq entityName (ssname selectedEntity 0)) ;assign entity name of entity at selected point to en
  (setq point2 (cdr (assoc 10 (entget entityName))))
  (setq point3 (cdr (assoc 11 (entget entityName))))
  (setq ang (rtd (angle point2 point3)))
 
  (if (and (> ang 90)(<= ang 270))
  (setq ang (- ang 180))
  ) ;end if
 
  (setq breakPoint1 (polar point1 (angle point3 point2) breakDistance))
  (setq breakPoint2 (polar point1 (angle point2 point3) breakDistance))
  (command "break" entityName breakPoint1 breakPoint2)
) ;end breakLine

Note: Yes, I do know of Lee's Automatick Block Break available here http://lee-mac.com/autoblockbreak.html but I would like to simply modify the existing code that we have to include polylines. Lee's routine is awesome but it would require a total reworking of the existing program.

Thank you
Any help is appreciated
~Greg

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Function to trim block on polyline as well
« Reply #1 on: March 26, 2013, 10:08:33 AM »
Try replacing your "breakline" function with this. I think it should work assuming the many global variables exist when calling.

Code: [Select]
(defun breakline (/ _getobjectangleatpoint)
  (defun _getobjectangleatpoint (ename pt / ang clpt e param)
    (if (and (not (vl-catch-all-error-p (vl-catch-all-apply 'vlax-curve-getendparam (list ename))))
     (setq clpt (vlax-curve-getclosestpointto ename pt))
     (setq param (vlax-curve-getparamatpoint ename clpt))
     (setq ang (angle '(0 0) (vlax-curve-getfirstderiv ename param)))
)
      ang
    )
  )
  (setq entityname (ssname selectedentity 0)) ;assign entity name of entity at selected point to en
  (setq ang (_getobjectangleatpoint entityname point1))
  (setq breakpoint1 (polar point1 ang breakdistance))
  (setq breakpoint2 (polar point1 (+ pi ang) breakdistance))
  (setq ang (rtd ang))
  (if (and (> ang 90) (<= ang 270))
    (setq ang (- ang 180))
  )
  (command "._break" entityname breakpoint1 breakpoint2)
)
« Last Edit: March 26, 2013, 10:42:30 AM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

trogg

  • Bull Frog
  • Posts: 255
Re: Function to trim block on polyline as well
« Reply #2 on: March 26, 2013, 10:32:47 AM »
Thank you for the response Ron.

I tested the Function that you posted. It works on vertical lines just fine, but it doesn't fully trim away on the horizontal lines and polylines

~Greg

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Function to trim block on polyline as well
« Reply #3 on: March 26, 2013, 10:43:08 AM »
I just made a change to the angle (+ pi ang) download and try again. Should work now.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

trogg

  • Bull Frog
  • Posts: 255
Re: Function to trim block on polyline as well
« Reply #4 on: March 26, 2013, 11:14:04 AM »
Thank you so much Ron.
It works beautifully

~Greg

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Function to trim block on polyline as well
« Reply #5 on: March 26, 2013, 11:26:55 AM »
 :-D

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC