Search code examples
geometry2dcoordinatesautocadautolisp

Adding points to an existing geometry


I have the dxf file of a 2D geometry created in AutoCAD and I would like to add points on the lines of the existing geometry. The list of points to be added is present in a text file. enter image description here

For example, I want to add 10 points on the line between 3 and 2 in the above image. The(x,y) coordinates of the 10 points are present in a text file. Is there a way to import and snap the coordinates close to the boundary(/line) because the coordinates come from a pre-processing step and might lie a bit off from the actual points on 3 - 2.

Any suggestions will be of great help!


Solution

  • Basicly it would be something like this:

     (defun C:ImportPoints (  / path coordinates askForFile loadPoints draw
     *error* )  (defun *error* ( msg / ) 
         (if (not (null msg ) ) (progn (princ "\nC:ImportPoints:*error*: " ) (princ msg ) (princ "\n")  ) )
     )
      (defun askForFile (  / 
          *error* ) (defun *error* ( msg / ) 
              (if (not (null msg ) )    (progn (princ "\naskForFile:*error*: " ) (princ msg ) (princ "\n")  ) )
          )
          (getfiled "Select the file with coordinates" "txt" 2)
      )
    
      (defun loadPoints ( path / 
          *error* ) (defun *error* ( msg / ) 
              (if (not (null msg ) )    (progn (princ "\nLoadPoints:*error*: " ) (princ msg ) (princ "\n")  ) )
          )  
    
        (setq filedesc (open Path "r"))
        (if filedesc(progn 
            (setq OutList (list))
            (setq linia(read-line filedesc))
            (while linia
            (progn
                (setq OutList (append OutList (list linia)))
                (setq linia (read-line filedesc))
            ))
            (close filedesc )
        ))  
        OutList
      )
    
      (defun draw ( coordinates / 
          *error* ) (defun *error* ( msg / ) 
              (if (not (null msg ) )    (progn (princ "\ndraw:*error*: " ) (princ msg ) (princ "\n")    ) )
          )
          (setq lastOSMODE (getvar "OSMODE"))
          (setvar "OSMODE" 512) ; 512 nearest
          (foreach % coordinates
            (command "_POINT" %)
          )
          (setvar "OSMODE" lastOSMODE ) ; ensure You didn't change anything in user settings
      )   
    
    
     (setq path (askForFile) )
     (setq coordinates (LoadPoints path) )
     (draw coordinates)
    

    )

    You have to know this is the basic "skeleton" of functionality. Main functions You have to use are:

    (getfiled ...) - which let You select the file

    (setvar "OSMODE" 512) - which make points will be on the line

    (command "_POINT" coordinates) which will draw the point The result will also depend on format of coordinates in the file. Expected is 2 or 3 coordinates separated by , and decimal should be separated by . I don't remember, but maybe it depends on You local settings.