Entmaking XLINEs

Some days ago Hutter7405 posted this question in the cadtutor.net forum:
I created an XLine function that when I type XV or XH puts a vertical or horizontal XLine on the Defpoints layer and automatically switches back to the previous layer. How do I make it so the function stays on Defpoints until you hit ESC then it returns to the original layer?
The function he created depended on calling the AutoCAD commands "_-LAYER" and "_XLINE" using the AutoLISP command function. So he had to create or set the "Defpoints" layer as current, draw the XLINEs and when finished, return to the previous layer. This can be done, but it is unnecessarily complicated.
A simpler way would be to use the AutoLISP entmake function that can create an entity on any layer even if it not the current one. So no need to change the current layer. But, in addition, when entmake creates an entity on a non-existent layer, the layer will be automatically created.

But maybe using entmake is too advanced for a newbie? No, it's not. We'll show how simple it is.
Entmake takes an entity list as argument to create the new entity. This entity list is what you can get, after drawing an XLINE by calling (entget (entlast)).
Figure 1. Obtaining an XLINE's entity list.

We can select the list returned on evaluating this expression in the Visual LISP Console, copy it and paste it into an Editor window. Using the Format edit window we can arrange this list in a column format like the one shown in Figure 2.
Figure 2. Formatted entity list.

Here we have commented (shown with a gray background) some of the sublists, which are not necessary for creating our XLINES. These are those associated with group codes -1, 330 and 5 which are specific to the previously drawn entity, and group codes 67 and 410 that refer to the Model/Paper space and the layout. If not included the entity will assume the current values. This also applies to group code 8, that specifies the layer. But this group code is the one that does the trick of creting the XLINE on the desired layer, in this case "Defpoints". The list shown in Figure 2 reads (8 . "0") as the entity we drew was created on the default layer "0".
Where do we find this information on the meaning of DXF group codes? This can be done pressing F1 to open the AutoCAD Help window and typing XLINE DXF in the search box for those codes that are specific for the XLINE entity or SOMETHING LIKE COMMON ENTITY DXF for those group codes like 67, 410 and 8 that are common to all of the graphic entities.

Figure 3. Information on DXF group codes in the AutoCAD help window. 

The other values that we'll have to provide are those associated with group code 10 that specifies the XLINE's midpoint, and group code 11 that must be a vector specifying the XLINE's orientation. So our function for drawing XLINEs on any layer will be very simple. It is shown in Figure 4.

Figure 4. Ent-XLINE function.

This function receives three arguments, pt for the group code 10 value, vec for the group code 11 value and lyr for the group code 8 value. After loading this function we can test it evaluating an expression like
(ent-xline '(100 100 0) '(1 0 0) "TEST")
which will create a horizontal XLINE in a layer called "TEST".

This XLINE will be aligned to the X axis if working in the WCS. But this will not be true if in a UCS where the axes orientation has been changed. So to draw Hutter7405's horizontal or vertical XLINES it will be safer to read the values for group code 11 from the UCSXDIR for the horizontal ones and UCSYDIR for the vertical ones.

The user can be prompted for the XLINE's position using the getpoint function. As this value must be specified in WCS values so we should foresee the possibility of a non WCS situation by using the trans function to convert the values from the current UCS to WCS values:
(trans pt 1 0)

This way we can define the two very simple functions shown in Figure 4, C:XV and C:XH, that call ent-xline satisfying the requested functionality.

Figure 4. New commands C:XV and C:XH.