brainstorming

How to trigger a python script from AutoCAD

Start-point

This has been achieved using AutoCAD 2017 and Python 3.5.

Python

First, I wrote a python script which will throw a pop up with a dumb message.

import ctypes
msg = "it's alive!"
ctypes.windll.user32.MessageBoxW(0, msg, "TEST POP UP"1)

The aim of this script is to check if AutoCAD is really executing the script. Thus, I need something that shows me it’s working. If I wouldn’t add this pop up (which waits for my input) the python shell would close so fast I would not feel any difference.

Save the script. I saved my file at C:/alive.py.

After testing the python script from Windows shell (cmd.exe) to ensure it does what I aimed, I jumped to AutoCAD.

AutoCAD

First I tried to open the python shell from AutoCAD. This is possible using the command start and typing the path of python shell executable C:\Users\<yourUsername>\AppData\Local\Programs\Python\Python35-32\python.exe. Hit ENTER and voilà! You are inside a python shell.

And finally I executed the python script from AutoCAD using the following AutoLISP lines:

(setq
    python "C:/Users/<yourUsername>/AppData/Local/Programs/Python/Python35-32/python.exe"
    pyscript "C:/alive.py"
)
(startapp python pyscript)

and once more… bingo! I triggered my first python script from AutoCAD!

Why?

This little test has its origin in my tiredness. I’m bored of being let down constantly by (startapp) AutoLISP function. This function is supposed to trigger an external app accepting some arguments, such us open windows explorer in a specific directory, etc. The problem is that I am not able to make it work as I want. So I thought, “hey, python might be able to accomplish this dumb task with its eyes closed, let’s give it a try, if this works it will open me a whole new world of toys!”. And it has done it, indeed 😛

Happy coding!

Advertisements
productivity

How to write math in atom

I find quite often I want to write few mathematical expressions, but diving into LaTeX might sound a bit overwhelming. As we say in Spanish, it’s like hunting flies with cannons

I describe below an open source solution which I believe will be really helpful for you.

Installation

You will need to

  1. Install the latest atom version from official website.
  2. Open atom and go to Settings (Ctrl + ,) > Install
  3. Install markdown-preview-enhanced package.
  4. Disable markdown-preview package. This package is installed by default after installing atom.

How to use it

Render in real time

The point of using this package is the possibility to render in real time TeX expressions together with markdown. To do so:

  1. Go to Settings (Ctrl + ,) > Packages
  2. Look for markdown-preview-enhanced package.
  3. Click the button Settings.
  4. Scroll down and check the box Live Update.

Writing

We are ready to write, we need to enable the Preview pane. Otherwise, you will just see your markdown code, instead of the render.

You can do this in three different ways:

  • Shortcut: hit Ctrl + Shift + M.
  • Command palette:
    1. Hit Ctrl + Shift + P to open the Command Palette.
    2. Type Markdown Preview Enhanced: Toggle and hit ENTER.
  • Right click menu: right click over the text of a markdown file and select the option Toggle markdown-preview-enhanced.

Examples

Inline

Code:

This is an inline latex formula. $\frac[x^2][b]$ Do you like it?

Output:

2017-04-14 12_06_49-markdown-latex.pdf - SumatraPDF

Block

Code:

This is an block latex formula, see below:
$
a_0+\cfrac{1}{a_1+\cfrac{1}{a_2+\cfrac{1}{a_3+\cdots}}}
$
Do you like it?

Output:

2017-04-14 12_07_09-markdown-latex.pdf - SumatraPDF

More stuff

Math Rendering Option

This option can be found in the package settings.

Change the Math Rendering Option to MathJax.

The package sets KaTeX by default. However, I found this package didn’t support \cfrac (and it sucks to use \frac for big fractions as they look pretty poor… ¬¬).

Exporting to PDF

To export your document to a PDF:

  1. Right click on the preview pane.
  2. Select Export to Disk.
  3. Select PDF
  4. Choose your settings and hit Export.

References

You can check the official repository of this package in GitHub for further information. There are much more things this package can do apart from rendering LaTeX.

Uncategorized

TRIM command | Advanced AutoCAD

Hi guys!

This weeks I have been very busy playing with screen recording, audio edition and video edition. But I’m back!

Have a look to my first video! Let me know on the comments any other AutoCAD topic you would like me to speak about.

Enable English subtitles (unless you want to practice your Spanish :P).

productivity

Madoko. Taking the writing to the next level

Have you ever felt you want to have your text in a versatile and light format and don’t find an easy way? This post is for you!

Some months ago I started to write reports to prepare myself to write reports for my chartership. Why prepare? Is it really necessary? Well… yes, definitely yes.

Preparation matters: true story…

As university student I have had to elaborate the Degree Final Projects (Bachelor’s, Master’s and Bachelor’s again), plus a long list of academic works, reports, lecture notes, presentations… and trust me, the preparation matters. There are many examples but styling is probably one of the most visual cases.

My final project reached the 1000 pages. Before submitting it I passed onto my director for a last check. Guess what? Image footers needed to be left aligned, instead of centre aligned. WTF?! Imagine to do that one by one? ONE BY ONE??? Luckily, I was using LibreOffice that allowed text hierarchy and style customization. This made the gargantuan reformatting task as long as 2 minutes as all the text within the document had perfectly defined categories associated with their corresponding style. I only needed to update the style associated to the image footers and done. If I had used a platform with a poor style management, such us the Windows Notepad I would have been in a serious problem.

This is the importance of the right choice in advance.

Requirements

Obviously, before choosing the right tool you need to decide what will you produce.

Is not the same to write a short report or a novel, use many mathematical expressions or not, need to draw diagrams or not, need to collaborate with more people or write on your own, need to export it to PDF or to a blog…

There are a pile of tools out there and each one is specialist in one field.

Madoko

In my case I usually don’t have very clear how I am going to export my document…maybe as a post? or I will keep it for me as a plain text note? or it might be used for a presentation?

This is a pain. I don’t want to decide at documents early stages what will I do with my document. Because usually I have no clue!

Madoko solves this (and many other) problem(s) for me. Madoko is based on Markdown. This means you write plain text, which brings many advantages:

  • Self readable: you can perfectly read it without any format.
  • Editability: it’s a plain text, you can open it with anything!
  • Longevity: you will always have a program to open your plain text.
  • Procesablity: as plain text can be easily imported and processed to carry out checkings, batch formatings, etc. or simply extract the information to use it somewhere else.

Till here, nothing different to Markdown. So, what does Madoko offer that Markdown doesn’t?

  • LaTeX full support: great if you are a LaTeX user. You can keep your formulas while writing in a much more friendly way. Is way much easier to read a Markdown code than a LaTeX code.
  • Multiple export: PDF + HTML + slides. You can export your document to a PDF in case you are looking to create a book or a printable document. You can also generate a website, or go even further and create a web-based presentation (which is great because you just need any browser to run it!).
  • CSS based style management.
  • Offline CLI or online GUI. The online GUI offers a nice editor with many options.
  • Dropbox, GitHub and other platform integration for synchronization.

how can I say it… it was love at first sight. Now I can focus on my content and worry about the formatting later on. I will always be able to access my information from anywhere, and doesn’t matter I don’t have internet, the “code” itself is so clear and clean you can read it easily without any render. I can issue my reports on PDF to print them and hand a hard copy to my mentor or simply upload an static HTML to a temporary repository and share the link with my mentor so that he can access it from anywhere (avoid paper waste!). I still can’t imagine where the slides might be useful for my chartership…but as we say in my mother tongue: everything will come.

EXTRA

Here the a video of the author, Daan Leijen, explaining Madoko’s capabilities:

productivity

How to boost your AutoCAD workflow

If you are reading this post you are probably an AutoCAD user who -like me and many others- wants to improve its drafting performance.

There are basically 2 wayls to speed up your AutoCAD workflow:

  1. Delegate. It might be worth to delegate drafting tasks to a specialised professional.
    Pros: forget about the process problems, just detail the desired output and it will be done for you.
    Cons: you need to pay.
  2. Automate. AutoCAD offers many ways to automate tasks: macros, custom commands, plug-ins…
    Pros: once built they are forever.
    Cons: you need to know how to code.

I know what are you thinking hold on, hold on… what are you suggesting? ¬¬ I don’t have time to learn to program and in my situation is not worth it to hire nobody to draw for me.

Here my proposal: delegate the automation. It will probably require a bigger starting cost, but you will recover the initial investment as you produce faster. Remember: an automation is forever.

productivity

AutoLISP: script #1

AutoLISP has become an essential tool in my job. Small scripts like the one I want to share with you now make me the life easier and help to ensure the things are done right (and way much faster). These scripts can also be used as many times as you need, so is worth it to invest some more time in them.

Case: I had to process hundreds of cheeky polylines which were supposed to be closed, but they weren’t. Instead, the first and the last vertexes of every polyline had the same coordinates.

In order not have duplicated vertexes, I needed to remove manually the first (or last) vertex and then close the polyline. I was not going to repeat that process manually, no way.

Solution:

Enjoy!

productivity

AutoCAD .NET API: the next step towards efficiency

I published not so long ago a post about scripting in AutoCAD to boost your productivity. Scripting is great! You don’t need to set up anything, AutoCAD is enough to start doing magic. And is pretty easy to update and test your code. Actually, the experience of discover and learn AutoLISP and VLISP during this year it has been amazing. However, the more scripts I was building, the more limitations I was finding, such as create custom objects.

So, a step towards a more powerful tool was unavoidable. .NET had to happen, in my case with C# (or CSharp). Why not VisualBasic? Why not ObjectARX (C++ based)? Well, despite I have played a bit with VisualBasic before to create some Macros, and I had some relation with C++, as I spent couple of years building Expert Advisors for MetraTrader with MQL4 and MQL5, I think C# is the best option to start. “Alright David, but you haven’t told us why…¬¬” Okey…okey! I basically looked for professional advice, a friend within the industry. The answer was straight and without any doubt C#.

So, here we are. After 2 weeks reading about C#, classes – I still don’t understand how could I write any code till today without knowing what was a class…-, namespaces, inheritance and a whole bunch of new and very interesting concepts, I took Visual Studio 2015 and created my first command AutoCAD 2017 (eh, debug included!).

I can’t wait to learn more and create my super cool drafting stuff!