Orientation and normal direction of BREPs

Hi!
So I started scripting a couple of months back now with a class about computational architecture and python scripting, our latest assignment consists of making a script to do half-lap joints on two sets of “timber” beams, one go which is crossing and the other the two beams are collinear.
My problem arises when trying to perform said joints on collinear beams that are angled, this comes in two steps, first if the beams are angled in only one direction, my script somehow find a cross product of the axis which should be collinear as well so a cross product should be equal to zero, initialising the part of the script that prompts the user to select the top surface in order to get its normal; second if I angle the beams one more time, meaning they are not aligned with any world axis the script breaks completely and I get an error message like so:
UnboundLocalError: local variable ‘length’ referenced before assignment
which doesn’t appear at all in other casses

Here’s said code: (hoping that’s how you upload code here I’m not sure at all this is my first post)

HalfLap_Joint.py (7.8 KB)

And here’s the 3dm file with the 3 steps of angles for the collinear beams:
05_Part2_START.3dm (2.3 MB)

Thanks in advance for your help and don’t hesitate if anything isn’t clear I’d be happy to provide any info I would’ve overlooked.

Haven’t looked at your code, but when you get an error message

local variable ‘blabla’ referenced before assignment

it means somehow your variable length hasn’t been assigned anything yet. Often this happens in conditionals where you assign something to it based on a certain condition, but if that condition doesn’t get met, you have forgotten to assign it an alternative value.

if x>0:
    length=25

If x<=0, and you try to access length later, you will get that message, as it’s only assigned the value in the conditional if x is positive.

if x>0:
    length=25
else:
    length=0

will work to assign a value to length no matter what and it will not error out later.

1 Like

Thanks, helps me understand a bit more of what’s happening but since the “length” variable entirely depends on the geometry at hand I would need to know why it doesn’t get assigned.
But hey I know that taking the time to look at the code and all is quite the demand so thanks already for your help :slight_smile:

If you just want to know when the length is not assigned, you could print “Length not assigned” instead of the length = 0 that Mitch suggested. Then you will know for sure that something is amiss and can go look for it. If you don’t put in an else statement the exception will remain forever a mystery.

1 Like

The only two lines where you actually assign a value to length are lines 80 and 146, and both are
length = edge.GetLength()

at line 77 you have the following:

    for edge in edges:
        if (edge.Duplicate().TangentAtEnd.IsParallelTo(normal) != 0): 
            length = edge.GetLength()
            break

you can see that there can be multiple conditions where length is not assigned anything…

The same is true at line 144:

    for edge in edges:
        if (edge.Duplicate().TangentAtEnd.IsParallelTo(normal) != 0): 
            length = edge.GetLength()
            break

You will need to decide what to assign to length (or do something else) in the cases where the condition isn’t met.

1 Like

Good detective work, Mitch.

Two more cases of not handling the whole picture in an If clause. Good practice dictates always using an If - ElseIf - Else clause. Same idea applies to Case clauses (or whatever it’s called in the language you’re using) which should always include an “other” or “finally” case. Automatically doing this causes the developer to consider all possibilities.

Consider: if nothing else can happen, why are you using an If anyway?

1 Like