At the start of your program you should store the current OSMODE value:
(SETQ curr_osmode (GETVAR "OSMODE"))
Before you begin drawing anything you should initialize your empty selection set ss1.
Move (SETQ ss1 (SSADD)) above the entity creation stuff in your code before your WHILE loop.
Then, every time you create a new entity, execute this code:
(SSADD (ENTLAST) ss1)
When you are done your selection set will have everything necessary for your PEDIT command:
(COMMAND ".PEDIT" "M" ss1 "" "Yes" "Join" 0.0)
As others have suggested, you should disable snaps in your programs whenever you are not manually picking the points. Even if you are manually picking points in your program it is better to set the OSMODE to exactly what is needed just prior to manually picking any point. You can use the command line overrides within your command call (e.g., "non", "end", "tan", "qua", "int" or multiples, "end,qua,int" etc.) or (SETVAR "OSMODE" ###) to do this before using the COMMAND function. (Look at OSMODE help to find the numbers you need or just set it via the dialog and use (GETVAR "OSMODE") to see the number for those settings.) Then, use (SETVAR "OSMODE" ###) just before points are acquired (manually picked) or used in your COMMAND call to set it to whatever is needed. DO NOT put the point selection inside your command call!
Use
(SETQ pnt# (GETPOINT)); or,
(SETQ pnt# (POLAR <pt> <angle> <distance>)); or,
(SETQ pnt# (INTERS <line-a pt1><line-a pt2><line-b pt1><line-b pt2> <nil or T>)); nil = lines treated as infinite length when checking if intersection is on both line segments.
Store each of the points as a separate symbol (variable name) and then put those in your command call.
(COMMAND ".LINE" pnt1 pnt2 "")
When you exit your program use (SETVAR "OSMODE" curr_osmode) to restore the osmode to what it was when you started. (Put this in your program's error function too if you have one.)
HTH
AutoCAD User since 1989. Civil Engineering Professional since 1983
Product Version: 13.6.1963.0 Civil 3D 2024.4.1 Update Built on: U.202.0.0 AutoCAD 2024.1.6
27.0.37.14 Autodesk AutoCAD Map 3D 2024.0.1
8.6.52.0 AutoCAD Architecture 2024