Quantcast
Channel: CAD Panacea - Autolisp
Viewing all 10 articles
Browse latest View live

Decrypt FAS Files?

$
0
0

I know this is a controversial topic, so let me clear the air right up front. This article is NOT a 'how-to' for decrypting or decompiling FAS files.

This topic seems to come up every now and then. Sometimes on the Autodesk newsgroups (where it's promptly deleted) or maybe on an unmoderated forum such as alt.cad.autocad. Either someone has "lost the .lsp file", and needs to decrypt the only thing left, the FAS file -or- someone is just openly looking for a way to get into someone else's protected FAS file. More reason to back up your source code, even print it out and store the hardcopy. Retyping is bad, but much better than the alternative.

Anyway, I have run across what I believe to be the only "FAS decrypter" out there. I base that on the fact that I have never seen one. I won't say how I obtained this program, or even the name of it in order to not feed the fire. But I can assure you I did not just "google it", nor did I even go in search of this tool at all. Let's just say I obtained it along with some other files (non .FAS, even non CAD related) from someone who probably didn't even know it was there. How did I know what this EXE was? The filename kind of gave it away.

Based on this thread in the autodesk.autocad.customization newsgroup, this topic has been around for awhile and by early 1999, FAS was already "cracked". I have no idea if that thread was referring to this tool or not.

Anyway, as an experiment, I tried to run this tool on one of my own FAS files, which is about 19k. Well about 30 minutes later, it was apparent that not much was being accomplished, at least based on the progress bar, so I stopped. No partial output file or anything.

I tried again on a much smaller file of mine, this time the file size is 949 bytes. Almost the same story, about 10 minutes later, I have to end task.

By this point, I'm thinking this program is a fake and not really doing anything. I decide to give it one more try. I write this one line of code (alert "hello") and save it as a LSP file. Then compile it into a FAS file which turns out to be a whopping 161 bytes.

7 minutes later, the program finishes. The output DOES contain the strings "Alert" and "Hello", but surrounded by a bunch of binary garbage. No indication of the original position of each string, or any parenthesis or quotation marks.

In summary, it appears that any decent size program would take hours to "decrypt" (if it would ever finish) - and that all you would have after you are done are the strings contained in the original LSP file. Not much else.

So if there is even a CHANCE you could lose your LSP source code, back it up today. Make a hard copy, whatever it takes. FAS file to source code doesn't look good at all.


Entmake HATCH entity

$
0
0

After working on some autolisp code where I needed to create several HATCH entities, I figured that using (entmake) to do the job would be much faster than pushing the command line version of the HATCH command.

I didn't have my own example of this, and after checking out the DXF reference, it looked a bit more complicated than other entities, so I set out in search of some sample code that worked.... which led me to a post by "CarlB" over in the CADTutor forums that he had posted a few years ago. This worked great until I added in the scale factor and changed the pattern from SOLID to ANSI31. After a few iterations of trial and error, I managed to make it do what I wanted.

When using a real pattern (as opposed to SOLID), you have to specify a few more parameters. Head over to this thread in the CADTutor forums for the example code.

As it turns out, using (entmake) is about 10 times faster than using the (command) function in this case.

EDIT: I failed to mention that the example posted at the link above is for hatching a simple boundary defined by four lines. If you have arcs or other entities involved, the code will be a bit different.

Entmake HATCH entity - Part 2

$
0
0

Regarding yesterday's post....what I failed to cover with any detail was the fact that the example code posted in the CADTutor forum post was strictly for a closed area defined by four lines. Of course that isn't the only area you may need to hatch. Today, I needed to adapt this code to hatch an area defined by an ellipse.

The AutoCAD DXF reference goes into a fair amount of detail, but not necessarily everything you need to know. What I have found over the last few days is the best advice comes from querying an existing entity and studying the return values. For an ellipse, you need to tell AutoCAD the center point of the ellipse, the distance from the center point to the upper quadrant, the ratio of the width to the height, and the angle of the imaginary elliptical arc. Remember, we are hatching an area, not necessarily an entity. The code ends up looking like this:

(entmake
  (list
	   (cons 0 "HATCH")
	   (cons 100 "AcDbEntity")
	   (cons 8 "0")
	   (cons 100 "AcDbHatch")
	   (cons 62 13)
	   (cons 10 cenpt)
	   (cons 210 (list 0.0 0.0 1.0))
	   (cons 2 "SOLID")
	   (cons 70 1)
	   (cons 71 0)
	   (cons 91 1)
	   (cons 92 1)
	   (cons 93 1)
           ; the "3" designates this is an elliptical shape	 
	   (cons 72 3)
           ; center point of ellipse
	   (cons 10 cenpt)   
           ; point of top quad
	   (cons 11 otherpt) 
           ; ratio of width to height
	   (cons 40 0.1)     
           ; start angle
	   (cons 50 0.0)     
           ; end angle (full ellipse)
	   (cons 51 (* pi 2.0))  
           ; counterclockwise flag
	   (cons 73 1)       
	   (cons 97 0)       
	   (cons 75 0)
	   (cons 76 1)
	   (cons 98 1)
	   (cons 10 (list 0.0 0.0 0.0))
	 )
)

If you are hatching using lisp and the slow (command) or (vl-cmdf) functions, you won't believe the speed difference until you see it. Maybe this will help someone else down the road.

Accessing Civil 3D objects with autolisp

$
0
0

I have been working with the Civil 3D 2010 API in visual lisp recently, and I thought I would share an example of working with an alignment object. This example has plenty of comments, but basically it shows you how to find a point near the alignment based on a station and offset. Then it does the opposite and shows you how to determine the station and offset, given a point. For clarity, I have left out most of the error checking.

; standard entity selection
(setq sel (entsel"\nSelect Alignment: "))
; get the entity name
(setq ent (car sel))
; get the entity list
(setq lst (entget ent))
; check to make sure the selection was the expected type
(if (eq"AECC_ALIGNMENT" (cdr (assoc 0 lst)))
  ; if so, convert the entity into a VLA-OBJECT
  (setq obj (vlax-ename->vla-object ent))
)
(if obj
  (progn; get the name of the alignment
    (setq nam (vlax-get-property obj 'Name))
    ; get the start station
    (setq sta1 (vlax-get-property obj 'StartingStation))
    ; get the end station
    (setq sta2 (vlax-get-property obj 'EndingStation))
    ; set a couple of variables
    (setq sta 100.0 off 10.0)
    ; using the above variables, find this point on the alignment
    (vlax-invoke-method obj 'PointLocation sta off 'x 'y)
    ; create an AutoCAD point at this location
    (entmake (list (cons 0 "POINT")(cons 10 (list x y))))
    ; ask the user to pick a point
    (setq pt1 (getpoint"\nSelect point: "))
    ; find the station and offset for this point
    (vlax-invoke-method obj 'StationOffset (car pt1) (cadr pt1) 'sta 'off)
    ; inform the user
    (alert (strcat"The station is "
            (rtos sta 2 2)
            "\nThe offset is "
            (rtos off 2 2)))
  )
)

Getting a list of polyline vertices using LISP

$
0
0

Here is one way to generate a list of polyline vertices using lisp. This function requires one argument, a polyline entity. The polyline can be the old style POLYLINE entity (2D or 3D), or it can be an LWPOLYLINE entity.

(defun getcoords (ent)
  (vlax-safearray->list
    (vlax-variant-value
      (vlax-get-property
	(vlax-ename->vla-object ent)
	"Coordinates"
      )
    )
  )
)

Example:

(getcoords (car (entsel)))

This function will return a list of coordinates. If the polyline is a LWPOLYLINE entity, the list will be in the form of (x y x y x y x y), and the number of vertices can be determined by dividing the length of the list by 2.

If the polyline is a POLYLINE entity (2D or 3D), then the list will be in the form of (x y z x y z x y z), and the number of vertices can be determined by dividing the length of the list by 3.

A sample of the output is shown below:
(-3.65553 2.56916 -0.83241 5.477 3.9698 3.23796 6.50188 4.54649 10.0235 2.71455 14.8257 6.78553)

Afralisp 2.0

$
0
0

If you have been around AutoCAD for a while and have done any Autolisp or VBA programming, you have probably run across a great Autolisp and VBA site with tutorials and code examples named AfraLisp. About 4 years ago, the Afralisp website, created by and formerly maintained by Kenny Ramage was taken over by David Watson, who also runs http://www.CadTutor.net.




David has just completed Afralisp v2.0, a total rework of the website. Everything has been reorganized, and the whole site has a fresh look. Stop by and take a look, and leave some feedback.

Get your name in lights

$
0
0

Ok, not exactly in lights, but in print! Seriously, do you have a time saving tip, technique, or code that you would like to share? Submit it to Cadalyst and have it published in Tips and Tools Weekly (subscribe here), or maybe in the monthly Hot Tip Harry feature.

Don't be shy, there are tips from reminders about commands to complex autolisp routines, and everything in between (yes, there is even some VBA code). If you are looking for a routine, you can also find almost 10 years of previously published code at http://cadtips.cadalyst.com. What if you are having problems with some existing code? Drop into the one of the many forums out there, including Augi, CadTutor, and of course the Hot Tip Harry forum at Cadalyst, and ask some of the pros.

Here is the Hot Tip Harry code for 2010: April 2010 | March 2010 | February 2010 | January 2010

Use the Bold link above to submit your tip/code or you can send it directly to harry@cadalyst.com

Changing sheet to display in PDF Underlay

$
0
0

When you attach a multi-page PDF file as a reference in AutoCAD, you can choose which sheet to display (see below).





But what if you want to change that reference to a different sheet in the PDF file, later on? There is nothing in the Ribbon to do this, and "Page Number" is a read-only property in the Properties Palette. You could detach the reference and add it again with the new sheet number, but the following lisp function is quicker.

The lisp function below allows you to pick the PDF Underlay object on the screen, and then enter a new sheet number. The image on the screen is updated as soon as the command completes. Make sure you choose a valid sheet. If the PDF file contains 9 sheets and you enter 10, then the PDF reference will disappear.

The function is named "Foo" (feel free to rename) and it calls the PdfPage function.


;|

R.K. McSwain, Copyright © 2011 - cadpanacea.com

|;

(defun PdfPage (ent pag / sel dat)  
  (setq dat (entget (cdr (assoc 340 (entget ent)))))
  (entmod (subst (cons 2 pag) (assoc 2 dat) dat))
  (entupd ent)
)

(defun c:Foo ( / s n)
  (setq s (car (entsel)))
  (setq n (getstring "\nEnter new page: "))
  (pdfpage s n)
  (princ)
)

What else can you think of, to automate?


Placing multi-sheet PDFs into AutoCAD, semi-automatically

$
0
0

With regard to last week's post on modifying the PDF sheet display, I've taken another approach and come up with the following lisp code that will allow you to import some or all of the sheets of a multi-sheet PDF all at once. I realize that the built-in PDFATTACH command allows you to place multiple sheets at once, but you can't see the sheets as you place them. This lisp code is also more of a "how-to" for use in larger routines perhaps - and because such, this is raw lisp code with no error checking. Feel free to dress it up and append the header.

Load the lisp file and then type in the command PMP. Select a multi-sheet PDF file, and then enter the number of sheets you want to insert (this should be equal or less than the total number of sheets in the PDF). At this point you can start picking the lower left corner for each sheet until you reach the end.

I suspect there are some PDF libraries available by which you could expand this further, by detecting the number of sheets and maybe even the page size - which would allow for a fully automated tool to place multiple sheets.


;|

R.K. McSwain, Copyright © 2012 - cadpanacea.com

|;

(defun C:PMP ( / fname cnt i)  
  (setq fname (getfiled "\nSelect Multisheet PDF File" (getvar "dwgprefix") "PDF" 0))
  (setq cnt (getint "\nNumber of sheets in this PDF to place ") i 1)
  (if fname
    (repeat cnt
      (vl-cmdf "-pdfattach" fname i (getpoint "\nPick LL point for next sheet ") 1.0 0.0)
      (setq i (1+ i))
    ) 
  )
  (princ)
)

Create a table style in AutoCAD using autolisp

Viewing all 10 articles
Browse latest View live