CAD Dimension

Hi Guys,

I’m new to python and scripting but I thought I would have a crack at writing an add-on which can add a dimension to 2 selected vertices.

I’ve been searching the net for snippets of code I might be able to use but I’m coming to the realisation that hacking something together may be a little harder than I thought.

So basically I can get the vertex coordinates, create a panel in the toolbar and write the distance in the different planes to this panel. But I’m sure that my code would make a programmer wince in pain if they saw it :no:.

What I would like this add-on to do is:

  • Update when different vertices are selected.
  • Have a button which adds a separate object (this would be the dimension arrows) and the text.
  • Update this dimension to the different axis/plane from the drop down menu.

Any advice/help/information would be greatly appreciated and sorry in advance for any noobie mistakes :o

import bpy
import math

from bpy.props import *

#Set mode to object mode 

#Get active vertex coordinates
vert_coordinates = [ for i in if == True]

#Set mode back to edit mode

#Function to determine vector magnitude
def mangnitude(x,y,z):
    w = math.sqrt(x*x + y*y + z*z)
    return w

#Determin the Dimension vector and magnitude
dimension_vector = vert_coordinates[0]-vert_coordinates[1]
dimension_vector_string = [str(round(x,2)) for x in dimension_vector]
dimension_magnitude = mangnitude(dimension_vector[0],dimension_vector[1],dimension_vector[2])

# Function to create different pannel types

def initSceneProperties(scn):
    bpy.types.Scene.MyInt = IntProperty(
        name = "Integer",
        description = "Enter an integer")
    scn['MyInt'] = 17
    bpy.types.Scene.MyFloat = FloatProperty(
        name = "Length",
        description = "Enter a Length",
        default = 1,
        min = -100,max = 100)
    scn['MyFloat'] = True
    bpy.types.Scene.MyBool = BoolProperty(
        name = "Face Camera",
        description = "True or False?")
    scn['MyBool'] = True
    bpy.types.Scene.DimensionAxis = EnumProperty(
        items = [('0', 'X',''),
            ('1', 'Y', ''),
            ('2', 'Z', ''),('3', 'Vector', '')], #<-- Not sure what the 3 values contained in the brackets mean. The middle one is the dispalyed text
        name = "Axis")
    scn['DimensionAxis'] = 3
    bpy.types.Scene.MyString = StringProperty(
        name = "String")
    scn['MyString'] = "Lorem ipsum dolor sit amet"

#I think this links the properties we defined above to the current scene???


#Create the UI Pannel
class UIPanel(bpy.types.Panel):
    bl_label = "Add Dimension"
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    def draw(self, context):
        layout = self.layout
        scn = context.scene
        #layout.prop(scn, 'MyInt', icon='BLENDER', toggle=True)
        #layout.prop(scn, 'MyFloat')
        #layout.prop(scn, 'MyBool')
        layout.prop(scn, 'DimensionAxis')
        #layout.prop(scn, 'MyString')
        if scn['DimensionAxis'] == 0:
            row = layout.row()
            row.label(text = 'Delta X : '+dimension_vector_string[0])
        if scn['DimensionAxis'] == 1:
            row = layout.row()
            row.label(text = 'Delta Y : '+dimension_vector_string[1])
        if scn['DimensionAxis'] == 2:
            row = layout.row()
            row.label(text = 'Delta Z : '+dimension_vector_string[2])
        if scn['DimensionAxis'] == 3:
            row = layout.row()
            row.label(text = 'Vector : '+str(round(dimension_magnitude,2)))

#Not quite sure whats happening below but it doesen't work without this part :)

class OBJECT_OT_PrintPropsButton(bpy.types.Operator):
    bl_idname = "idname_must.be_all_lowercase_and_contain_one_dot"
    bl_label = "Print props"
    def execute(self, context):
        scn = context.scene
        printProp("Int: ", 'MyInt', scn)
        printProp("Float: ", 'MyFloat', scn)
        printProp("Bool: ", 'MyBool', scn)
        printProp("Enum: ", 'DimensionAxis', scn)
        printProp("String: ", 'MyString', scn)
def printProp(label, key, scn):
        val = scn[key]
        val = 'Undefined'
    print("%s %s" % (key, val))

# Registration