Jump to content

Recommended Posts

Posted

I'm trying to make code to insert an attribute block at the midpoint of a line.
The value / content in the attribute block is taken from the .txt file

I've tried to make it correctly, but when I run the code I get an error: no function definition

 

this is the code I use :

(defun C:N-K ()
 (if (setq fname (findfile "list of text.txt"))
   (progn
     (setq file (open fname "r")
    l nil
     )
     (while (setq line (read-line file))
       (setq l (append l (list line)))
     )
     (close file)

     (setq i (load_dialog "insertmidpoint.dcl"))
     (if (not (new_dialog "notation" i)) (exit))

     (setq elev(get_tile "userinput")
               room (nth 0 l)
         )

          (start_list "mylist" 3)
          (mapcar 'add_list l)
          (end_list)

          (set_tile "mylist" (setq rtn "0"))
          (action_tile "mylist" "(setq rtn $value)")
          (action_tile "accept" "(setq l (mapcar '(lambda (x) (nth x l)) (read (strcat \"(\" (get_tile \"mylist\") \")\")))) (done_dialog)")
          (action_tile "cancel" "(done_dialog 0) (setq i nil)")
          (start_dialog)

	    (progn

	    (setvar "AUNITS" 3)

	    (setq Lines (ssget '((0 . "LINE,POLYLINE,LWPOLYLINE"))))

	    (repeat (setq Ros (sslength Lines))
	      
	    (setq Lin (ssname Lines (setq Ros (- Ros 1))))

            (foreach room l

	      (setq p1 (get-line-midpoint Lin))

	      (setq oAttreq(getvar 'attreq))
	      (setq oAttdia(getvar 'attdia))
	  
    	      (setvar 'attreq 1)
           	      (setvar 'attdia 0)

	      (command "-INSERT" "att.def" p1 1 1 (* 1 (GetAngle0 Lin )) room)

	      (setvar 'attreq oAttreq)
	      (setvar 'attdia oAttdia)
            )

	      ); rep
	    )
          )       
        )
      )
    )
  )

  (princ)

)

(defun get-line-midpoint (line)
  ; (setq line Lin)

      (setq start_pt (cdr (assoc 10 (entget line))))
      (setq end_pt (cdr (assoc 11 (entget line))))
  
      (findfile (/ (+ (car start_pt) (car end_pt)) 2.0)
            (/ (+ (cadr start_pt) (cadr end_pt)) 2.0))
    
  
)

    ; Abgle units :Decimal Degree

(defun GetAngle0( linge / Vxlinge Pst Pfn x1 x2 y1 y2 DX DY)    ; Fct 1
  ;return pente of a line
  (setq Vxlinge (vlax-ename->vla-object linge)
	Pst (vlax-curve-getStartPoint Vxlinge)
	Pfn (vlax-curve-getEndPoint Vxlinge))

  (angle Pst Pfn)

  
  ) ; defun

(defun C:GetPente() (setq ggg (GetPente (car (entsel)))))


 

Posted
(defun C:GetPente() (setq ggg (GetPente (car (entsel)))))

 

In the code above there is no function GetPente

 

 

(setq elev(get_tile "userinput")

would be better with a space in between elev(get_tile

(setq elev (get_tile "userinput")

 

 

I'll often comment out code if I can't work out what is going wrong - starting at the beginning so only a few lines run, and add a few lines back in at a time until I find the error, might halp with debugging especially things like no function definition or typing errors in variables

  • Agree 1
Posted
21 minutes ago, Steven P said:
(defun C:GetPente() (setq ggg (GetPente (car (entsel)))))

 

In the code above there is no function GetPente

The problem I get is error: no function definition: GET-LINE-MIDPOINT

do you have any suggestions what should I do?

Posted

If it was me I'd put all the (defun c :..... functions at the end of the file so everything is loaded before you run them (just my preference, not necessary in this case)

 

Try changing this:

(defun get-line-midpoint (line)

to add spaces: 

(defun get-line-midpoint ( line / )

 

 

weekend so CAD is off here to test but try that. I'll often put in the / for my reference that I know there are no localised variables if there is nothing there

Posted
1 hour ago, Kvlar said:

The problem I get is error: no function definition: GET-LINE-MIDPOINT

do you have any suggestions what should I do?

Too many issues.

You have two extra brackets in the first routine: N-K

Then the mid-point function won't work on polylines because they don't have the DXF GC 11 as end points and that works only with line objects.

....... etc

  • Like 1
Posted
33 minutes ago, Tharwat said:

Too many issues.

You have two extra brackets in the first routine: N-K

Then the mid-point function won't work on polylines because they don't have the DXF GC 11 as end points and that works only with line objects.

....... etc

Wow, it seems like I made a lot of mistakes that I didn't know about.
Can you help me by telling me which parts I need to fix?

Posted

The brackets are easy, go through the code and note which bracket close which command. Should be clearer then which are the extra ones.

I'd also put which variables are localised ones (the ones only used in each function) in the definition line after the / that often fixes a lot of errors

 

After that try the code again and see where the errors are.

 

 

Could also post sample files to make this run for testing

Posted
18 minutes ago, Steven P said:

The brackets are easy, go through the code and note which bracket close which command. Should be clearer then which are the extra ones.

I'd also put which variables are localised ones (the ones only used in each function) in the definition line after the / that often fixes a lot of errors

 

After that try the code again and see where the errors are.

 

 

Could also post sample files to make this run for testing

is this what you mean?

 

(defun C:N-K ( / fname file l i elev rtn Lines Ros Lin p1 oAttreq oAttdia start_pt end_pt Vxlinge ggg )

 

I tried creating a local variable but the problem is still the same, namely error: no function definition: GET-LINE-MIDPOINT

Posted
1 hour ago, Tharwat said:

Too many issues.

You have two extra brackets in the first routine: N-K

Then the mid-point function won't work on polylines because they don't have the DXF GC 11 as end points and that works only with line objects.

....... etc

but I tried the code only on lines, not on polylines

Posted
28 minutes ago, Steven P said:

The brackets are easy, go through the code and note which bracket close which command. Should be clearer then which are the extra ones.

I'd also put which variables are localised ones (the ones only used in each function) in the definition line after the / that often fixes a lot of errors

 

After that try the code again and see where the errors are.

 

 

Could also post sample files to make this run for testing

Below I have attached the LSP, DCL & TXT files, who knows, you might be able to help solve my problem.

Thank You

 

 

files.rar

Posted

Don't suppose you can upload them separately?

Posted

I'll start with this, commented the closing brackets.

 

(c:getpente code is still wrong

 

(defun C:N-K ( / )
  (if (setq fname (findfile "list of text.txt"))
    (progn
      (setq file (open fname "r")
            l    nil
      ) ; end setq
      (while
        (setq line (read-line file))
        (setq l (append l (list line)))
      ) ; end while
      (close file)

      (setq i (load_dialog "insertmidpoint.dcl"))

      (if (not (new_dialog "notation" i))
        (exit)
      ) ; end if

      (setq elev (get_tile "userinput")
            room (nth 0 l)
      ) ; end setq

      (start_list "mylist" 3)
        (mapcar 'add_list l)
      (end_list)

      (set_tile "mylist" (setq rtn "0"))
      (action_tile "mylist" "(setq rtn $value)")
      (action_tile "accept" "(setq l (mapcar '(lambda (x) (nth x l)) (read (strcat \"(\" (get_tile \"mylist\") \")\")))) (done_dialog)")
      (action_tile "cancel" "(done_dialog 0) (setq i nil)")

      (start_dialog)
      (progn
        (setvar "AUNITS" 3)
        (setq Lines (ssget '((0 . "LINE,POLYLINE,LWPOLYLINE"))))
        (repeat (setq Ros (sslength Lines))
          (setq Lin (ssname Lines (setq Ros (- Ros 1))))
          (foreach room l
            (setq p1 (get-line-midpoint Lin))
            (setq oAttreq(getvar 'attreq))
            (setq oAttdia(getvar 'attdia))
            (setvar 'attreq 1)
            (setvar 'attdia 0)
            (command "-INSERT" "att.def" p1 1 1 (* 1 (GetAngle0 Lin )) room)
            (setvar 'attreq oAttreq)
            (setvar 'attdia oAttdia)
          ) ; end foreach
        )  ; end repeat
      ) ; end progn
    ) ; end progn
  ) ; end if
  (princ)
)

(defun get-line-midpoint ( line / )
  ;(setq line Lin)
  (setq start_pt (cdr (assoc 10 (entget line))))
  (setq end_pt (cdr (assoc 11 (entget line))))
  (findfile (/ (+ (car start_pt) (car end_pt)) 2.0)
            (/ (+ (cadr start_pt) (cadr end_pt)) 2.0)
  ) ; end findfile
)

;Angle units :Decimal Degree
(defun GetAngle0 ( linge / Vxlinge Pst Pfn x1 x2 y1 y2 DX DY)    ; Fct 1
  ;return pente of a line
  (setq Vxlinge (vlax-ename->vla-object linge)
	Pst (vlax-curve-getStartPoint Vxlinge)
	Pfn (vlax-curve-getEndPoint Vxlinge)
  ) ; end setq
  (angle Pst Pfn)
) ; defun

(defun C:GetPente( / ggg ) (setq ggg (GetPente (car (entsel)))))

 

Posted (edited)

I use Notepad ++ when writing code and it has lisp support so shows open and closed brackets including dcl "{}" I use it all the time.

 

Like Steven make sure defun are loaded just me I put them at start. 

 

Re midpoint you need to check is it a line or a pline then for line can use dxf 10 & 11, for a pline can use GETPOINTATDIST where distance = (Get-length/2.0).

 

(If (= (vlax-get obj 'name) "AcDbLINE")
(progn
do line stuff
)
(
do pline stuff
)
)

 

Ps please use ZIP easier to open.

Edited by BIGAL
Posted

findfile is AutoLISP function and is used to operate with strings : "filename.ext" and not with 2 real numbers like you posted... Instead of (findfile) I suppose you meant to use (list numb1 numb2)...

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