Author Topic: Hatch by polyline Question  (Read 15908 times)

0 Members and 1 Guest are viewing this topic.

V-Man

  • Bull Frog
  • Posts: 343
  • I exist therefore I am! Finally Retired!
Hatch by polyline Question
« on: August 23, 2005, 09:40:14 AM »
Hiya guys,

I'm trying to write a lisp to count how many polylines there are in a drawing and based on that info create a hatch individually for each. How can I accomplish this?

I have this so far....

Code: [Select]

(defun C:HP ()
(command "_undo" "_g")
  (setq ss (ssget "X" '((-4 . "<AND")(0 . "LWPOLYLINE")
                                 (-4 . "AND>"))))
  (if ss
    (progn
      (setq n (1- (sslength ss)))
        (repeat n
           (command ".hatch" "ANSI31" "200" "" (ssname ss n)))))
   (command "._undo" "_end")
  (princ)
)


Any thoughts definately welcome!
AutoCAD 9 - 2023, AutoCADMap 2008 - 2010, Revit 2012 - 2022, Autocad Civil 3D 2023

whdjr

  • Guest
Hatch by polyline Question
« Reply #1 on: August 23, 2005, 10:08:24 AM »
Well I think in order to hatch a polyline it needs to be closed, so a check for that should be included in your ssget list:  (70 . 1).

You really don't need the <ANDs in your list unless you are polling for the same dxf number.

whdjr

  • Guest
Re: Hatch by polyline Question
« Reply #2 on: August 23, 2005, 10:13:52 AM »
Quote from: dvarino
Code: [Select]

(defun C:HP ()
(command "_undo" "_g")
  (setq ss (ssget "X" '((-4 . "<AND")(0 . "LWPOLYLINE")
                                 (-4 . "AND>"))))
  (if ss
    (progn
      (setq n (1- (sslength ss)))
        (repeat n
           (command ".hatch" "ANSI31" "200" "" (ssname ss n)))))
   (command "._undo" "_end")
  (princ)
)


Another way you could write the IF statement without using 'prog' is like this
Code: [Select]
(if ss
  (repeat (setq n (sslength ss))
    (setq n (1- n))
    (command ".hatch" "ANSI31" "200" "" (ssname ss n))
  )
)


Just another way to do the same thing. :)

V-Man

  • Bull Frog
  • Posts: 343
  • I exist therefore I am! Finally Retired!
Hatch by polyline Question
« Reply #3 on: August 23, 2005, 10:20:56 AM »
Ok, this is what I got now.

Code: [Select]

Command: (defun C:HP ()
(_> (command "_undo" "_g")
(_>   (setq ss (ssget "X" '(0 . "LWPOLYLINE")(70 . 0)))
(_> (if ss
((_>   (repeat (setq n (sslength ss))
(((_>     (setq n (1- n))
(((_>     (command ".hatch" "ANSI31" "200" "" (ssname ss n))
(((_>   )
((_> )
(_>    (command "._undo" "_end")
(_>   (princ)
(_> )


I get this when i run the routine.

; error: bad argument type: consp 0
AutoCAD 9 - 2023, AutoCADMap 2008 - 2010, Revit 2012 - 2022, Autocad Civil 3D 2023

V-Man

  • Bull Frog
  • Posts: 343
  • I exist therefore I am! Finally Retired!
Hatch by polyline Question
« Reply #4 on: August 23, 2005, 10:24:36 AM »
Ah, found my mistake

Code: [Select]

(defun C:HP ()
(command "_undo" "_g")
  (setq ss (ssget "X" '((0 . "LWPOLYLINE")(70 . 0))))
(if ss
  (repeat (setq n (sslength ss))
    (setq n (1- n))
    (command ".hatch" "ANSI31" "200" "" (ssname ss n))
  )
)
   (command "._undo" "_end")
  (princ)
)


But this till will not hatch....
AutoCAD 9 - 2023, AutoCADMap 2008 - 2010, Revit 2012 - 2022, Autocad Civil 3D 2023

whdjr

  • Guest
Hatch by polyline Question
« Reply #5 on: August 23, 2005, 10:29:51 AM »
Your ssget function has to be passed a list:
Code: [Select]
yours:  (setq ss (ssget "X" '(0 . "LWPOLYLINE") (70 . 0)))

mine:   (setq ss (ssget "X" '((0 . "LWPOLYLINE") (70 . 0))))

whdjr

  • Guest
Hatch by polyline Question
« Reply #6 on: August 23, 2005, 10:30:30 AM »
(70 . 1)

whdjr

  • Guest
Hatch by polyline Question
« Reply #7 on: August 23, 2005, 10:38:21 AM »
Ok here is the problem.  You need an extra return at the end of your command statement:

Code: [Select]
(command ".hatch" "ANSI31" "200" "" (ssname ss n) "")

whdjr

  • Guest
Hatch by polyline Question
« Reply #8 on: August 23, 2005, 10:42:52 AM »
Just remember that

(70 . 0)  selects non-closed polylines (however the ending segments still need to cross for the hatch to display)

and

(70 . 1) selects closed polylines.

If you want it to do both the just take out the (70 . ?) all together.

V-Man

  • Bull Frog
  • Posts: 343
  • I exist therefore I am! Finally Retired!
Hatch by polyline Question
« Reply #9 on: August 23, 2005, 01:07:41 PM »
Thanks for the help, much appreciated. I knew that I was close, I just needed a kick in the head....

Thnx again
AutoCAD 9 - 2023, AutoCADMap 2008 - 2010, Revit 2012 - 2022, Autocad Civil 3D 2023

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
Hatch by polyline Question
« Reply #10 on: August 23, 2005, 04:40:54 PM »
Just to throw in my $0.02.....if a Pline's linetypegen is set to on, bit 128 is added to the group 70 code......you really should use it like this:
Code: [Select]

(setq ss (ssget "x" '((0 . "POLYLINE,LWPOLYLINE")(-4 . "&")(70 . 1))))
The "&" forces a bitwise comparison looking for the 1 bit.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Hatch by polyline Question
« Reply #11 on: August 23, 2005, 05:16:38 PM »
Provided for fun, as is, complete with warts & freckles ...

Code: [Select]
(defun c:HatchEm ( / _HatchIt ss i space )

    (defun _HatchIt ( space object / hatch err )
        (setq err
            (vl-catch-all-apply
               '(lambda ()
                    (setq hatch
                        (vlax-invoke
                            space
                           'AddHatch
                            acHatchStyleNormal ;; pattern type                
                            "ANSI31"           ;; pattern name
                            :vlax-false        ;; associativity
                            AcHatchObject      ;; hatch object type
                        )
                    )    
                    (vlax-invoke
                        hatch
                       'AppendOuterLoop
                       (list object)
                    )  
                    (vlax-invoke hatch 'Evaluate)
                )    
            )    
        )
        (if (vl-catch-all-error-p err)
            (princ
                (strcat
                    "Entity handle:"
                    (vla-get-handle object)
                    " caused this error: "
                    (vl-catch-all-error-message err)
                )
            )    
        )    
    )
   
    (cond
        (   (setq ss
                (ssget
                   '(   (0 . "lwpolyline,polyline")
                        (-4 . "&")
                        (70 . 1)
                    )
                )
            )
            (setq space
                (vlax-get-property
                    (vlax-get-property
                        (vlax-get-acad-object)
                       'ActiveDocument
                    )
                    (if (eq 1 (getvar "cvport"))
                       'PaperSpace
                       'ModelSpace
                    )
                )
            )    
            (repeat (setq i (sslength ss))
                (_HatchIt
                    space
                    (vlax-ename->vla-object
                        (ssname ss (setq i (1- i)))
                    )
                )
            )
        )
    )
   
    (princ)

)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

dubb

  • Swamp Rat
  • Posts: 1105
Hatch by polyline Question
« Reply #12 on: August 23, 2005, 05:27:45 PM »
that was a great code...

**i might be hijacking but,

how do i make it so that i draw some lines first then the hatch automatically goes in. without having to create a polyline and then running the routine.

LE

  • Guest
Hatch by polyline Question
« Reply #13 on: August 23, 2005, 06:28:42 PM »
Quote from: dubb
how do i make it so that i draw some lines first then the hatch automatically goes in. without having to create a polyline and then running the routine.


Are you thinking of vlisp reactors???

dubb

  • Swamp Rat
  • Posts: 1105
Hatch by polyline Question
« Reply #14 on: August 23, 2005, 06:46:48 PM »
Quote from: LE
Quote from: dubb
how do i make it so that i draw some lines first then the hatch automatically goes in. without having to create a polyline and then running the routine.


Are you thinking of vlisp reactors???


well im a noob to lisp..but i have developed a few programs.

will this type of routine that im talking about, require the use of reactors? i hear reactors are difficult to work with.