drawing line close to polyline - autocad

I have a Triangle as a polyline and I want to draw an Altitude. I give the command "line" or "_line" the right points but AutoCAD draws the line from the vertex to the adjacent vertex.
It's not just in AutoLISP, AutoCAD won't let me draw a line from a vertex to the middle of an edge in a polyline.
How can I do that?
I thought of adding a vertex in the polyline, but this didn't help that much. I tried to add connector: a circle or another object close
enough to the line and connect the altitude to it, but that didn't help either.
Any suggestions?

Firstly, I suspect that the issue with your current attempts is the influence of active Object Snap modes when supplying points to the LINE command.
There are several ways to avoid this:
1. Use the "None" Object Snap modifier
When supplying points to an AutoCAD command through an AutoLISP command expression, you can avoid the effect of any active Object Snap modes by preceding the point with the none or non Object Snap modifier.
This is similar to how you might precede a point with end to force the activation of the Endpoint Object Snap modifier, but in this case, none or non means "ignore all Object Snap modes". The full list of available prefixes may be found here.
Here is an example of this method:
(setq p '(0.0 0.0 0.0)
q '(1.0 1.0 0.0)
)
(command "_.line" "_non" p "_non" q "")
A few notes on the above:
The underscore prefixes _ (as used in "_.line" and "_non") cause AutoCAD to interpret the input as non-localised command input (i.e. English), else, if such input were supplied to a non-English version of AutoCAD, it may carry another meaning in the non-English language.
The dot/period prefix . (as used in "_.line") causes AutoCAD to always use the original definition of the command, and not a redefined version (as may exist if the user has used the UNDEFINE command).
2. Temporarily Disable Object Snap
Whilst the above method acknowledges that there may be active Object Snap modes, forcing such modes to be ignored for every point input, if you are issuing many command calls involving numerous point inputs, you may find it cleaner to simply temporarily disable Object Snap altogether, and then reenable the previously active modes following completion of your program.
The obvious way to achieve this would be to store the current value of the OSMODE system variable, set such system variable to 0 before issuing your command expression, and then reset the OSMODE system variable to its previous value, e.g.:
(setq p '(0.0 0.0 0.0)
q '(1.0 1.0 0.0)
m (getvar 'osmode) ;; Store current OSMODE
)
(setvar 'osmode 0) ;; Set OSMODE to 0 (disables all snaps)
(command "_.line" p q "")
(setvar 'osmode m) ;; Reset OSMODE to stored value
However, this method has the disadvantage that if an error occurs during the time in which OSMODE is set to 0, in the absence of an appropriate error handler, OSMODE will remain equal to 0 and the user will be apoplectic when they discover they have lost their Object Snap settings.
Therefore, a more 'graceful' way to temporarily disable OSMODE is to make use of bit 16384 which, when set, indicates that Object Snap has been turned off.
Now, rather than using an if statement to test whether bit 16384 is present in the OSMODE value, and if so, subtract it from the value, we can make use of the AutoLISP logior (inclusive bitwise OR) function to account for both scenarios in a single expression:
(setvar 'osmode (logior 16384 (getvar 'osmode)))
This will return the result of a bitwise inclusive OR operation between bit 16384 and the current value of the OSMODE system variable. Therefore, if 16384 is already present in the value, it will be returned, else it will be added.
This can be implemented as follows:
(setq p '(0.0 0.0 0.0)
q '(1.0 1.0 0.0)
m (getvar 'osmode) ;; Store current OSMODE
)
(setvar 'osmode (logior 16384 m)) ;; Turn off Object Snap
(command "_.line" p q "")
(setvar 'osmode m) ;; Reset OSMODE to stored value
Now, if the code encounters an error whilst Object Snap is turned off, the Object Snap settings are not lost - the user may simply need to turn Object Snap back on using F3.
3. Avoid Command Calls Altogether
Of course, the most bulletproof way of avoiding the influence of Object Snap on command point input is to avoid commands entirely!
Instead, you can use the entmake or entmakex function to append the DXF data to the drawing database directly:
(setq p '(0.0 0.0 0.0)
q '(1.0 1.0 0.0)
)
(entmake (list '(0 . "LINE") (cons 10 p) (cons 11 q)))
Or, you can follow the Visual LISP ActiveX route and use the AddLine method of the relevant Block container, e.g. to create a Line in Modelspace you might use:
(vl-load-com)
(setq p '(0.0 0.0 0.0)
q '(1.0 1.0 0.0)
)
(vla-addline
(vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object)))
(vlax-3D-point p)
(vlax-3D-point q)
)

Related

SICP The Picture Language Exercise Lambda Argument

(define (segments->painter segment-list)
(lambda (frame)
(for-each
(lambda (segment)
(draw-line
((frame-coord-map frame)
(start-segment segment))
((frame-coord-map frame)
(end-segment segment))))
segment-list)))
Some of you have already seen this sicp example before but for the first timers a brief explanation: segments->painter procedure takes arguments from segment list which has values are segments made up from vectors. for-each procedure acts like map but instead returns a list of value, it does operations like printing or in this code it draws lines. frame-coord-map scales frames according to unit square frame. What I don't understand in this code is where does first lambda function takes its argument (frame) from.
Here is a case of currying.
In your code
(define (segments->painter segment-list)
(lambda (frame)
...))
when you call segments->painter, scheme will return an object of some type, let us call figure. When you apply a graphical frame on this object, scheme will be able to draw your object.
(define obj1 (segments->painter seg1))
obj1 is an object of type figure and this object is able to receive frames. It is able to do only one action when it receives a frame, namely to get drawn in that frame: (obj1 frame1), (obj1 frame2), etc are actions you can do on that object.
Before to apply the graphical data on some object, it does make sense to talk about that object but it does not make sense to talk about a real representation of that object. In your case, the object can do one single action: to get drawn.
Between calling the contructor for the figure object and drawing the object you can do some (static) preprocessing, such that in the moment when you call the drawing method, you have already precomputed something.
This is how types are implemented in programming languages. This Peter Henderson's language is a first introduction to types and to combinators. This will be developped further in the 4th chapter, where the interpreter uses some precomputed data to execute (it converts the list input format to internal format).
Let's look at a simpler example:
(define (adder x)
(lambda (y)
(+ x y))
The analogous question would be, "Where does y come from, if you can call adder with only x?" As discussed in the comments, it comes from whoever calls the lambda that adder returns. You may write:
(define plus5 (adder 5))
(plus5 8) ; 13
(plus5 1) ; 6
At the point that you call adder, there is no y. In the next call, y is 8, and after that it is 1.
Likewise in your actual code, there is no frame until someone calls the returned lambda.

Move cursor to the given point value in emacs

Short story: Given a position value in the buffer say, 12345, how to take the cursor to the position directly
Long story: when i debug my emacs initial boot messages, it prints an error message as,
eval-buffer(#...........................) ; Reading at buffer position 19352
The fact, no line numbers are printed & only the position value is there makes my navigation tough. any clues, to make my cursor to jump to the position 19352?
Simply
(goto-char 19352)
See documentation
To enter Lisp code interactively, the eval prompt is M-:
You want to use that using the command goto-char, which by default is bound to M-g c. So you can do
M-g c 19352 RET
or, if you forget the binding,
M-x goto-char RET 19352 RET

What is the semantic difference between defining a name with and without parentheses?

(Though this is indeed simple question, I find sometimes it is common mistakes that I made when writing Scheme program as a beginner.)
I encountered some confusion about the define special form. A situation is like below:
(define num1
2)
(define (num2)
2)
I find it occurs quite often that I call num2 without the parentheses and program fails. I usually end up spending hours to find the cause.
By reading the r5rs, I realized that definition without parenthesis, e.g. num1, is a variable; while definition with parenthesis, e.g. num2, is a function without formal parameters.
However, I am still blurred about the difference between a "variable" and "function".
From a emacs lisp background, I can only relate above difference to similar idea as in emacs lisp:
In Emacs Lisp, a symbol can have a value attached to it just as it can
have a function definition attached to it.
[here]
Question: Is this a correct way of understanding the difference between enclosed and non-enclosed definitions in scheme?
There is no difference between a value and a function in Scheme. A function is just a value that can be used in a particular way - it can be called (as opposed to other kinds of value, such as numbers, which cannot be called, but can be e.g. added, which a function cannot).
The parentheses are just a syntactic shortcut - they're a faster, more readable (to experts) way of writing out the definition of the name as a variable containing a function:
(define (num)
2)
;is exactly the same as
(define num
(lambda () 2) )
The second of these should make it more visually obvious that the value being assigned to num is not a number.
If you wanted the function to take arguments, they would either go within the parentheses (after num, e.g. (num x y) in the first form, or within lambda's parentheses (e.g. (lambda (x y)... in the second.
Most tutorials for the total beginner actually don't introduce the first form for several exercises, in order to drive home the point that it isn't separate and doesn't really provide any true functionality on its own. It's just a shorthand to reduce the amount of repetition in your program's text.
In Scheme, all functions are values; variables hold any one value.
In Scheme, unlike Common Lisp and Emacs Lisp, there are no different namespaces for functions and other values. So the statement you quoted is not true for Scheme. In Scheme a symbol is associated with at most one value and that value may or may not be a function.
As to the difference between a non-function value and a nullary function returning that value: In your example the only difference is that, as you know, num2 must be applied to get the numeric value and num1 does not have to be and in fact can't be applied.
In general the difference between (define foo bar) and (define (foo) bar) is that the former evaluated bar right now and foo then refers to the value that bar has been evaluated to, whereas in the latter case bar is evaluated each time that (foo) is used. So if the expression foo is costly to calculate, that cost is paid when (and each time) you call the function, not at the definition. And, perhaps more importantly, if the expression has side effects (like, for example, printing something) those effects happen each time the function is called. Side effects are the primary reason you'd define a function without parameters.
Even though #sepp2k has answered the question, I will make it more clearer with example:
1 ]=> (define foo1 (display 23))
23
;Value: foo1
1 ]=> foo1
;Unspecified return value
Notice in the first one, foo1 is evaluated on the spot (hence it prints) and evaluated value is assigned to name foo1. It doesn't evaluate again and again
1 ]=> (define (foo2) (display 23))
;Value: foo2
1 ]=> foo2
;Value 11: #[compound-procedure 11 foo2]
1 ]=> (foo2)
23
;Unspecified return value
Just foo2 will return another procedure (which is (display 23)). Doing (foo2) actually evaluates it. And each time on being called, it re-evaluates again
1 ]=> (foo1)
;The object #!unspecific is not applicable.
foo1 is a name that refers a value. So Applying foo1 doesn't make sense as in this case that value is not a procedure.
So I hope things are clearer. In short, in your former case it is a name that refers to value returned by evaluating expression. In latter case, each time it is evaluated.

Why is this variable not set?

When evaluated from a scratch buffer, why does the elisp debugger say that variable "a" is void?
(setq a "b")
(insert a)
The reported error is:
Debugger entered--Lisp error: (void-variable a)
You must have forgotten to do a C-j. With it you also
get the feedback with the result of evaluation inserted into the buffer.
I'd also suggest to use ielm.
UPD
Here's the whole sequence, just in case:
emacs -q - don't want any extra packages to mess up anything
q - you're now in *scratch*
(setq a "b")
C-j - "b" should appear automatically
(insert a)
C-j - bnil should appear automatically
Done. The value of a was inserted, along with nil,
which was the result returned from insert.

(basic) Elisp programming: Special hook for Cut function in Emacs

I have a text file which has related content/paragraphs in it. Let's say that I Cut/Kill paragraph A from the text. I want to write a function that is invoked in this case and therefore the related paragraph - let's say B - is also removed. A good example would be a document that has citations/references in it. - i.e. whenever you remove that text the citation is also removed - something like what MS Office does. Theoretically I think:
1) I need a hook for Cut - which I can't find the appropriate hook so far
2) A search function with regex probably - to find the related text
3) remove that text
Can you advice me how to proceed? Hints for each step or etc.
It's easy to write functions for such cases:
(defun when-one-kill-one-and-three ()
"If a buffer has a string \"one\", it deletes it.
If in this buffer exists also a string \"three\", it will be killed afterwards. "
(interactive "*")
(save-excursion
(save-restriction
(widen)
(goto-char (point-min))
(while (search-forward "one" nil t 1)
(kill-region (match-beginning 0) (match-end 0))
(when (search-forward "three")
(kill-region (match-beginning 0) (match-end 0)))))))
1) I don't think there is a hook for Cut but you can have advised Cutting (defadvice ..)
2) You should somehow markup your text and find it with search (put a number so you search for that) ..
3) To remove text you can use kill-region I suppose

Resources