FLATTEN Unknown command "FLATTEN" not working lsp

FLATTEN Unknown command "FLATTEN" not working lsp

sohaybMX6R7
Participant Participant
795 Views
10 Replies
Message 1 of 11

FLATTEN Unknown command "FLATTEN" not working lsp

sohaybMX6R7
Participant
Participant

I'm experiencing an issue with the FLATTEN command in a LISP script. While the command works perfectly when entered manually in the command line, it returns the error when executed through my LISP code.

'Applying FLATTEN..._FLATTEN Unknown command "FLATTEN". Press F1 for help.' 

Environment:

  • AutoCAD 2025
  • Express Tools installed and functioning
  • FLATTEN command works correctly through manual input

I've verified that:

  1. Express Tools are properly installed
  2. The FLATTEN command works when typed manually
  3. The script runs without any other errors in another laptop
  4. I tried to downgrade from 2025 to 2024 but still the same issue

Has anyone encountered this issue before? Is there a specific way to call Express Tool commands in LISP scripts that I'm missing?

Any help would be greatly appreciated."

here's my code:

(defun c:FLATTENOBJ ()
(setq ss (ssget)) ; Prompt user to select objects
(if ss
(progn
(princ "\nApplying FLATTEN...")
(command "_FLATTEN" ss "") ; Apply FLATTEN command with selection set
)
(princ "\nNo objects selected."))
(princ))

0 Likes
796 Views
10 Replies
Replies (10)
Message 2 of 11

paullimapa
Mentor
Mentor

I believe this has been asked before but here's the answer.

As you're aware Flatten is part of Express Tools and it's a lisp function.

So to call a lisp function within another lisp function you'll have to do this:
(c:flatten)

But examining your code why would you need to create another lisp function called c:flattenobj which requests for selecting of objects to flatten when the Express Tools Flatten command already does this?

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 3 of 11

sohaybMX6R7
Participant
Participant

I am trying to implement it in another function, but always having this issue. so I simplified it to ask for it here
I am wondering why this code worked properly in another laptop with the same autocad version

0 Likes
Message 4 of 11

paullimapa
Mentor
Mentor

Perhaps you're mistaken on using the exact code on another laptop because your original code will always fail with exactly what you posted:

"Unknown command "FLATTEN". Press F1 for help."


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 5 of 11

sohaybMX6R7
Participant
Participant

thank you for your answer, (c:flatten) worked for me but how can I add the suffix for FLATTEN command, to automatically apply the function on the last selected item and also to remove hidden lines without asking the user to enter Y manually
something like: (command "_FLATTEN" ss "" "Y" "")

0 Likes
Message 6 of 11

paullimapa
Mentor
Mentor

Because Flatten is a lisp function you cannot use lisp to respond to the request.

You can however use a Script file...but that's another story.

Now if the Flatten function is the last thing to run in your entire code then the following modifications will work:

(defun c:FLATTENOBJ ()
(setq ss (ssget)) ; Prompt user to select objects
(if ss
(progn
(princ "\nApplying FLATTEN...")
; (command "_FLATTEN" ss "") ; Apply FLATTEN command with selection set
(vl-load-com)
(vla-Sendcommand (vla-Get-ActiveDocument (vlax-Get-Acad-Object)) "_FLATTEN _P  _Y\r")
)
(princ "\nNo objects selected."))
(princ))

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 7 of 11

cadffm
Consultant
Consultant

Solved, but you can't have enough solutions

 

Click -flatten subroutine

Sebastian

0 Likes
Message 8 of 11

sohaybMX6R7
Participant
Participant

thank you this solution worked with me, but I am trying to make a block from the flattened objects, i am unable to select them, how can i select the flattened objects automatically and create a block from them ?

0 Likes
Message 9 of 11

gbattinPH5TG
Advocate
Advocate

I heard through a recent post that the Expresstools are no longer automatically loaded in 2025. So similar to running (vl-load-com) in order to use COM features, TYou may have to run this in your code:

(acet-load-expresstools)
(vl-load-com)
;;; 2025 doesn't load the express tools automatically anymore.
;;; so some lisp functions that rely on express tool functions
;;; will need to add (acet-load-expresstools) similar to how (vl-load-com)
;;; needs to be added.
;;; some people suggest adding this line to the ACAD.lsp or acaddoc.lsp

0 Likes
Message 10 of 11

sigmmadesigner
Advocate
Advocate

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun C:FL (/ tmpucs olderr oldcmd zeroz ss1 ss1len
i numchg numnot numno0 ssno0 ename elist
etype yorn vrt crz
)
(setq tmpucs "$FLATTEN-TEMP$") ;temporary UCS

;;Error handler
(setq olderr *error*)
(defun *error* (msg)
(if (or
(= msg "Function cancelled")
(= msg "quit / exit abort")
)
;;if user cancelled or program aborted, exit quietly
(princ)
;;otherwise report error message
(princ (strcat "\nError: " msg))
)
(setq *error* olderr)
(if (tblsearch "UCS" tmpucs)
(command "._UCS" "_Restore" tmpucs "._UCS" "_Delete" tmpucs)
)
(command "._UNDO" "_End")
(setvar "CMDECHO" oldcmd)
(princ)
)

;;Function to change Z coordinate to 0

(defun zeroz (key zelist / oplist nplist)
(setq oplist (assoc key zelist)
nplist (reverse (append '(0.0) (cdr (reverse oplist))))
zelist (subst nplist oplist zelist)
)
(entmod zelist)
)
;;Setup
(setq oldcmd (getvar "CMDECHO"))
(setvar "CMDECHO" 0)
(command "._UNDO" "_Group")
(command "._UCS" "_Delete" tmpucs "._UCS" "_Save" tmpucs "._UCS" "_World")
;set World UCS

;;Get input
(prompt
(strcat
"\nFLATTEN fija la coordenada Z de los objetos en Z=0."
)
)

(initget "Yes No")
(setq yorn (getkword "\nQuieres continuar? <Y>: "))
(cond ((/= yorn "No")
(graphscr)
(prompt "\nEscoge los objetos que quieras bajar a Z=0 ")
(prompt
"[Pulsa return para seleccionar todos los objetos del dibujo]"
)
(setq ss1 (ssget))
(if (null ss1) ;if enter...
(setq ss1 (ssget "_X")) ;select all entities in database
)


;;*initialize variables
(setq ss1len (sslength ss1) ;length of selection set
i 0 ;loop counter
numchg 0 ;number changed counter
numnot 0 ;number not changed counter
numno0 0 ;number not changed and Z /= 0 counter
ssno0 (ssadd) ;selection set of unchanged entities
) ;setq

;;*do the work
(prompt "\ntrabajando.")
(while (< i ss1len) ;while more members in the SS
(if (= 0 (rem i 10))
(prompt ".")
)
(setq ename (ssname ss1 i) ;entity name
elist (entget ename) ;entity data list
etype (cdr (assoc 0 elist)) ;entity type
)

;;*Keep track of entities not flattened
(if (not (member etype
'("3DFACE" "ARC" "ATTDEF"
"CIRCLE" "DIMENSION" "ELLIPSE"
"HATCH" "INSERT" "LINE"
"LWPOLYLINE" "MTEXT" "POINT"
"POLYLINE" "SOLID" "TEXT"
)
)
)
(progn ;leave others alone
(setq numnot (1+ numnot))
(if (/= 0.0 (car (reverse (assoc 10 elist))))
(progn ;add it to special list if Z /= 0
(setq numno0 (1+ numno0))
(ssadd ename ssno0)
)
)
)
)

;;Change group 10 Z coordinate to 0 for listed entity types.
(if (member etype
'("3DFACE" "ARC" "ATTDEF" "CIRCLE"
"DIMENSION" "ELLIPSE" "HATCH" "INSERT"
"LINE" "MTEXT" "POINT" "POLYLINE"
"SOLID" "TEXT"
)
)
(setq elist (zeroz 10 elist) ;change entities in list above
numchg (1+ numchg)
)
)

;;Change group 11 Z coordinate to 0 for listed entity types.
(if (member etype
'("3DFACE" "ATTDEF" "DIMENSION" "LINE" "TEXT" "SOLID")
)
(setq elist (zeroz 11 elist))
)

;;Change groups 12 and 13 Z coordinate to 0 for SOLIDs and 3DFACEs.
(if (member etype '("3DFACE" "SOLID"))
(progn
(setq elist (zeroz 12 elist))
(setq elist (zeroz 13 elist))
)
)

;;Change groups 13, 14, 15, and 16
;;Z coordinate to 0 for DIMENSIONs.
(if (member etype '("DIMENSION"))
(progn
(setq elist (zeroz 13 elist))
(setq elist (zeroz 14 elist))
(setq elist (zeroz 15 elist))
(setq elist (zeroz 16 elist))
)
)

;;Change each polyline vertex Z coordinate to 0.
;;Code provided by Vladimir Livshiz, 09-Oct-1998
(if (= etype "POLYLINE")
(progn
(setq vrt ename)
(while (not (equal (cdr (assoc 0 (entget vrt))) "SEQEND"))
(setq elist (entget (entnext vrt)))
(setq crz (cadddr (assoc 10 elist)))
(if (/= crz 0)
(progn
(zeroz 10 elist)
(entupd ename)
)
)
(setq vrt (cdr (assoc -1 elist)))
)
)
)

;;Special handling for LWPOLYLINEs
(if (member etype '("LWPOLYLINE"))
(progn
(setq elist (subst (cons 38 0.0) (assoc 38 elist) elist)
numchg (1+ numchg)
)
(entmod elist)
)
)

(setq i (1+ i)) ;next entity
)
(prompt " Done.")

;;Print results
(prompt (strcat "\n" (itoa numchg) " objecto(s) bajados a Z=0."))
(prompt
(strcat "\n" (itoa numnot) " object(s) no modificados.")
)

;;If there any entities in ssno0, show them
(if (/= 0 numno0)
(progn
(prompt (strcat " ["
(itoa numno0)
" with non-zero base points]"
)
)
(getstring
"\nPulsa enter para ver los objetos de Z no nula que o han sido cambiados... "
)
(command "._SELECT" ssno0)
(getstring "\nPulsa enter para desseleccionarlos... ")
(command "")
)
)
)
)

(command "._UCS" "_Restore" tmpucs "._UCS" "_Delete" tmpucs)
(command "._UNDO" "_End")
(setvar "CMDECHO" oldcmd)
(setq *error* olderr)
(princ)
(princ)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(princ)

0 Likes
Message 11 of 11

Sea-Haven
Mentor
Mentor

Some times with express tools you need to call a sub defun an example is EXTRIM but to use in a lisp (ETRIM obj pt) 

 

I found an old copy of flatten.lsp.

 

(defun c:flatten ( / ss ans )
 (acet-error-init (list nil 1))
 
this may be a problem if no ACET loaded

 

Not sure what current Flatten.lsp looks like or wether it uses ACET functions.

0 Likes