Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 09/21/2026 in all areas

  1. I put in to Claude Opus 5. Attached is the result. It took a couple of minutes or so, with one revision. LTCOUNT_1.lsp
    1 point
  2. J2lstaples, I have been using AI a bit lately, When I code a bigger project even small ones I write numerous defuns the reason being they solve one task but a program may need 5 task defuns. Also I can test that defun and make sure its working correctly rather having one great big program and trying to find where it is not working. But sometimes the AI just does not work for a defun task so have to go back to searching old fashioned way. You can use AI but you have to know how to describe a task, knowing who some lisp functions like SSget work with filters. Not sure what AI would think of this, I will do old fashioned way. "Drag line over multiple parallel p/Lines get two out side linework, offset both outward further make a box, now ssget text on a layer within that box. Also get how many lines, Then sort and count the text multiplied by the number of lines and put results in a table." Yes it is a current post request, not here at Cadtutor. I am interested in what AI can do and how long it takes to make something that works.
    1 point
  3. Though I would quite often check because I would need to change the PRECISION or make sure the INSUNITS are correctly set by using the UNITS command, I would seldom check the actual drawing units by using the -DWGUNITS command. I would say it's most important to make sure the current units are recognized by AutoCAD as mm or inches and etc than if it's Architectural/Engineering/Decimal.
    1 point
  4. I am curious to understand what the problem is. Right now, I am working 400 km away from home and only have access to an old PC running Windows 10 and AutoCAD 2002. I must say that this code actually works for me. I will try testing it soon on a more recent version of AutoCAD, and then perhaps I’ll add a further comment.
    1 point
  5. @GLAVCVSI just tried commenting out that line as you recommended but still no joy. The freezing up now also occurs on the Win 11 OS. Even after I unfreeze the Select Files window, I still cannot Cancel out but I must make a file selection to dismiss the window. The behavior just seems very unstable with a persistent powershell setup at least on the systems I tested at my end. But thanks for your feedback & code revisions. Perhaps others here can use this faster alternative if it's stable running the code this way on their systems.
    1 point
  6. The last line of code (pl:getfiledps "Hola" "c:\\" "dwg" 1) is for testing purposes and should not be loaded at startup. Comment it out, save the changes, and open a new drawing to reload the code. Once the file has loaded, you can execute (pl:getfiledps "Hola" "c:\\" "dwg" 1). The reason for this is that PowerShell runs asynchronously, and `pl:getfiledps` might execute before the PowerShell instance is available.
    1 point
  7. You can start a PowerShell instance from LISP and keep it running in the background, ready to receive instructions. I've played around with this idea a little and ended up with the following code. I haven't tested it thoroughly enough, so its robustness could probably be improved. But it may be useful. (defun PS-START (/ fp sh cmd n) (vl-load-com) ;| -------------------------------------------------------------- ; Si existe READY, suponemos de momento que ya está funcionando. ; Posteriormente podemos hacer PS-ALIVE más rigurosa. |; -------------------------------------------------------------- (setq *PS-SERVER* (strcat (getenv "TEMP") "\\PL_PS_Server.ps1") *PS-TCMD* (strcat (getenv "TEMP") "\\PL_PS_Command.tmp") *PS-CMD* (strcat (getenv "TEMP") "\\PL_PS_Command.txt") *PS-OUT* (strcat (getenv "TEMP") "\\PL_PS_Output.txt") *PS-READY* (strcat (getenv "TEMP") "\\PL_PS_Ready.txt") ) (if (and *PS* (findfile *PS-READY*)) T (progn ;|Clean previous files / Limpiar archivos anteriores|; (if (findfile *PS-CMD*) (vl-file-delete *PS-CMD*) ) (if (findfile *PS-OUT*) (vl-file-delete *PS-OUT*) ) ;|Create server script / Crear script servidor|; (setq fp (open *PS-SERVER* "w")) ;|Nombres de los archivos de comunicación|; (write-line "$cmd = $env:TEMP + '\\PL_PS_Command.txt'" fp) (write-line "$out = $env:TEMP + '\\PL_PS_Output.txt'" fp) (write-line "$ouTmp = $env:TEMP + '\\PL_PS_Output.tmp'" fp) (write-line "$ready = $env:TEMP + '\\PL_PS_Ready.txt'" fp) ;|Indicate Powershelll ready / Señalizar que PowerShell está preparado|; (write-line "Set-Content -Path $ready -Value $PID -Encoding Default" fp) ;|Main loop / Bucle principal Powershell remains here for entire session / PowerShell permanece aquí durante toda la sesión|; (write-line "while ($true) {" fp) ;|Is there any pending request? / ¿Hay una orden pendiente?|; (write-line " if (Test-Path $cmd) {" fp) ;|Read ALL code / Leer TODO el código. -Raw permite que PS-EXEC pueda mandar scripts de varias líneas, no solamente una instrucción |; (write-line " $code = Get-Content -Path $cmd -Raw -Encoding Default" fp) ;|Delete request inmediately / Borrar la petición inmediatamente. Esto indica que el servidor ya se ha hecho cargo de ella.|; (write-line " Remove-Item $cmd -Force" fp) ;|Orden para terminar el servidor|; (write-line " if ($code.Trim() -eq '__EXIT__') {" fp) (write-line " break" fp) (write-line " }" fp) ;|Execute code IN THIS POWERSHELL SESSION / Ejecutar código EN ESTA MISMA SESIÓN POWERSHELL|; (write-line " try {" fp) (write-line " $result = Invoke-Expression $code" fp) ;| OUT must always be created so that PS-EXEC knows the process has finished Hay que crear siempre OUT. Incluso si la expresión devuelve $null necesitamos crear el archivo para que PS-EXEC sepa que hemos terminado. |; (write-line " if ($null -eq $result) {" fp) (write-line " Set-Content -Path $ouTmp -Value '' -Encoding Default" fp) (write-line " }" fp) (write-line " else {" fp) (write-line " $result | Out-File -FilePath $ouTmp -Encoding Default" fp) (write-line " }" fp) (write-line " Move-Item $ouTmp $out -Force" fp);rename PL_PS_Output.tmp / renombramos PL_PS_Output.tmp ;|Capturar errores|; (write-line " }" fp) (write-line " catch {" fp) (write-line " ('__ERROR__ ' + $_.Exception.Message) | Out-File -FilePath $out -Encoding Default" fp) (write-line " }" fp) ; close / cerrar 'IF Test-Path' (write-line " }" fp) ;| Sleep / Demorar 10 ms. Prevents resident Powershell instance from continuously using a CPU core while it has nothing to do Evita que el PowerShell residente utilice continuamente un núcleo de CPU mientras no tiene nada que hacer |; (write-line " Start-Sleep -Milliseconds 10" fp) ; cerrar WHILE (write-line "}" fp) ;|PowerShell está terminando|; (write-line "Remove-Item $ready -Force -ErrorAction SilentlyContinue" fp) (close fp) ;|Arrancar PowerShell|; (if (not *PS*) (progn (setq cmd (strcat "powershell.exe " "-NoLogo " "-NoProfile " "-NonInteractive " "-STA " "-ExecutionPolicy Bypass " "-WindowStyle Hidden " "-File \"" *PS-SERVER* "\"" ) sh (vlax-create-object "WScript.Shell") ) (vlax-invoke-method sh 'Run cmd 0 :vlax-false) (vlax-release-object sh) ) ) ;| Esperar a que PowerShell cree READY Máximo: 5 segundos |; (setq n 0) (while (and (not (findfile *PS-READY*)) (< n 500) ) (repeat 10000 (getvar "MILLISECS")) (princ "\rEsperando...") (setq n (1+ n)) ) ;|Result / Resultado|; (if (findfile *PS-READY*) T nil ) ) ) ) (defun PS-EXEC (code / fp lin result t0) ;| Clears any previous result / Borrar posible resultado anterior|; (if (findfile *PS-OUT*) (vl-file-delete *PS-OUT*) ) ;| Write code that Powershell should executed / Escribir el código que debe ejecutar PowerShell|; (setq fp (open *PS-CMD* "w")) (write-line code fp) (close fp) ;|Waiting for a Powershell response / Esperar a que PowerShell responda|; (while (not (findfile *PS-OUT*));(not (findfile *PS-OUT*)) (setq t0 1000) (repeat 1000 (setq t0 (1+ t0))) ) ;|Reading response / Leer respuesta|; (setq fp (open *PS-OUT* "r"));(open *PS-OUT* "r")) (while (setq lin (read-line fp)) (setq result (cons lin result)) ) (close fp) ;| Delete result / Borrar resultado|; (vl-file-delete *PS-OUT*) (reverse result) ) (defun PL-FILEDIALOG-INIT (/ hwnd) ;; HWND de ESTA instancia concreta de AutoCAD (setq hwnd (vla-get-HWND (if (minusp (- (atoi (getvar "ACADVER")) 16)) (vla-get-activeDocument (vlax-get-acad-object)) (vlax-get-acad-object))) ) ;; Windows Forms + OpenFileDialog (PS-EXEC (strcat "Add-Type -AssemblyName System.Windows.Forms;" "$script:f=New-Object System.Windows.Forms.OpenFileDialog" ) ) ;|Small 'wrapper' to convert AutoCAD's HWND into a valid 'IWin32Window' for ShowDialog(owner) Pequeño 'wrapper' para convertir el HWND de AutoCAD en un 'IWin32Window' válido para 'ShowDialog (owner)' |; (PS-EXEC (strcat "Add-Type -TypeDefinition " "'using System;" "using System.Windows.Forms;" "public class WindowWrapper:IWin32Window{" "private IntPtr h;" "public WindowWrapper(IntPtr hwnd){h=hwnd;}" "public IntPtr Handle{get{return h;}}" "}' " "-ReferencedAssemblies System.Windows.Forms" ) ) ;|Locate AutoCAD and create owner Localizar AutoCAD y crear el 'owner' |; (PS-EXEC (strcat "$script:owner=New-Object WindowWrapper(" (itoa hwnd) ")" ) ) T ) (defun pl:getfiledps (title dir ext flg / psquote makefilter code) ;| Escape / Escapar ' PowerShell|; (defun psquote (s / p n) (setq n 0) (while (setq p (vl-string-search "'" s n)) (setq s (strcat (substr s 1 p) "''" (substr s (+ p 2))) n (+ p 2) ) ) s ) (defun makefilter (s / p r x) (setq r "") (while (setq p (vl-string-search ";" s)) (setq x (substr s 1 p) r (strcat r "*." x ";") s (substr s (+ p 2)) ) ) (strcat r "*." s) ) ;|Default values / Valores por defecto|; (if (/= (type title) 'STR) (setq title "Select Files") ) (if (not (and (= (type dir) 'STR) (vl-file-directory-p dir) ) ) (setq dir (getvar "DWGPREFIX")) ) (if (= (type ext) 'STR) (setq ext (makefilter ext)) (setq ext "*.dwg") ) ;| Build only the code needed for this call: Powershell, Forms, f and owner ALREADY EXISTS Construir únicamente el código necesario para ESTA llamada: PowerShell, Forms, f y owner YA EXISTEN |; (setq code (strcat "$script:f.FileName='';" "$script:f.Title='" (psquote title) "';" "$script:f.InitialDirectory='" (psquote dir) "';" "$script:f.Filter='Files (" ext ")|" ext "';" "$script:f.Multiselect=" (if (and (= (type flg) 'INT) (/= flg 0) ) "$true;" "$false;" ) "$r=$script:f.ShowDialog($script:owner);" "if($r -eq " "[System.Windows.Forms.DialogResult]::OK)" "{$script:f.FileNames}" ) ) ;| Execute in the resident Powershell instance / Ejecutar en PowerShell residente |; (PS-EXEC code) ) ;|If Powershell is not already running, start a instance / SI POWERSHELL NO ESTÁ INSTANCIADO, SE CREA UNA INSTANCIA|; (if (not *PS*) (progn (setq *PS* (PS-Start)) (PL-FILEDIALOG-INIT))) ;;;(startapp "notepad" *PS-OUT*) (setq *PS* nil) ;;;(pl:getfiledps "Hola" "c:\\" "dwg" 1) How does it work?... When the code is loaded, a PowerShell instance and a base script are created. After that, simply call the pl:getfiledps function whenever you need it.
    1 point
  8. Here's a simple workaround. When you get a drawing from someone else, open a New drawing (from one of your templates). Xref their drawing into that one and Bind it as an insertion. That gives you a block, which you can edit to clean it up and then explode. If there's a scale problem, it will show up there. Voila! You have their drawing with all your settings applied.
    1 point
×
×
  • Create New...