onsdag 29. oktober 2014

Definitions in code blocks for dummies

Up to this point I've not realized the possibilities of using definitions in codeblocks. However, it can be quite useful!
In an earlier post I showed you a definition that read textfile based coordinates into Revit:

However, it's tedious to repeat the large codeblock over and over, and though I haven't figured out how to repeat the operation automatically based on list length, I've managed to make a definition in a code block that, when called, can be repeated more easily:

So I will try to explain what parts make up a callable defintion. (By callable I mean a seperate standing code block that we can "call" from another codeblock.)

Red= by typing "def" you tell dynamo that this codeblock will be a callable codeblock
Blue= This is the name that you later will call from another code block.
Green= Green contains all the outer variables that will have to be inputted to execute the definition (The left side of dynamo nodes)
Orange= this is the name of the variable.
Yellow= The colon means that whatever follows will define what kind of variable the "list"-variable will be.
Cyan= this defines the variable "list", in this case "var[][]" means a "list of lists of variables"
Pink= In a code block definition "return" is the output of the definition and must be included
Purple= "{" marks the start of the definition and "}" marks the end

If you want multiple variables you seperate them by commas inside the parentheses

The rest is the "body", that would be the calculations and stuff that will be executed based on the inputs.

So when I call this node in an ordinary code block I would type: Curvebyind(x). (Notice that I don't have to use "list" as i did in the definition)

A variable can be many things but I've listed some of them here, including an error that comes if you put a floating number(3.145) into a function that expects an integer:

PS: If this is way below your level of dynamo expertise, I've left something out or ,even worse, written something entirely wrong, then I hope you get provoked enough to supplement in the comments! :)

onsdag 8. oktober 2014

Text based coordinate files and how to import them to Revit

In Norway we have a format for coordinates that is used by surveyors, consultants and others alike. It has the file extension .kof and I do not think it's very widespread outside our fiordinfested country

Basically this is a text format which have seperators for substrings or subvalues. Like this:
In the example over, 09_91 marks start of a line and 09_99 marks end of line.
Coordinate block (05) - point number (1037) - Theme code(2201) -Xvalue - Yvalue -Zvalue

By doing some prep work before putting this into dynamo, one could take this via excel and importing into dynamo that way, but where is the fun in that?

With some intense googling, cursing and doing other hair loosening activities I found the keys to my problem: Python and the commands re.compile, find.all and DO NOT forget re.dotall*

However, the problem was to find, extract and divide ALL substrings between 09_91 and 09_99, not just the first and last or every nth item.

So here is the syntax:

And that produces this:

Which means we have separated out each specific string between 09_91 and 09_99.
(I guess I could have done this with inputs for other seperators as well rather than writing it as string directly in the code.)
Not entirely sure I've figured out the best way to go from here, but depending on the string structure one could compose a function like this:

From here on I'm not sure what to do. The goal have to be minimum input by others who will use this definition, and in this workflow one would have to duplicate the steps in the first codeblock pr list instance between 09_91 and 09_99. This is very tedious for large coordinatefiles. Hopefully someone have the solution for a more iterative method! :)

As per now it serves the purpose, but it will have to be polished to work as it should.

If anyone have tips or comments on how to solve the iterative part, please do not hesitate to contact me! :)

*re.DOTALL makes the find.all function find all string characters between 09_91 and 09_99 INCLUDING those on new lines in the string, without it: No results but a empty list...

torsdag 2. oktober 2014

Preview dynamo and bridges

Playing around in Revit with divided lines, adaptive profiles and dynamo. What a tool box! Hope to be able to share something more soon: