[SOLVED] TechDraw: Align dimensions

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
RedTorch
Posts: 40
Joined: Mon Nov 07, 2022 12:23 am

[SOLVED] TechDraw: Align dimensions

Post by RedTorch »

Hi,

I'm trying to get started with my first script...

Basically, how do I get all the selected objects? (BTW I'm in TechDraw)


Thanks

Code: Select all

import FreeCAD
from FreeCAD import Base
import math, collections
from PySide import QtGui, QtCore


def get_selection():
	''' check selection of View and ObjectList '''
	OK, ObjectList, View = False, [], []
	if Gui.Selection.getCompleteSelection():
		View = Gui.Selection.getCompleteSelection()[0]
		ObjectList = Gui.Selection.getSelectionEx()[0].SubElementNames
		if len(ObjectList) > 0:
			OK = True
	return (OK,ObjectList,View)
	#return ObjectList

o = get_selection()
print(o)
Last edited by RedTorch on Sun Nov 20, 2022 11:13 am, edited 2 times in total.
RedTorch
Posts: 40
Joined: Mon Nov 07, 2022 12:23 am

Re: Newbie

Post by RedTorch »

It's rough but I think it works...

In TechDraw, select multiple Horizontal or Vertical dimensions, this will then set their position the same

*set where you want the label before using script!

Code: Select all


import FreeCAD
from FreeCAD import Base
import math, collections
from PySide import QtGui, QtCore


print("\nDIMENSION ALIGN\n")

def get_selection():
	View = []
	if Gui.Selection.getCompleteSelection():
		View = Gui.Selection.getCompleteSelection()
	return View

o = get_selection()
#print(o)
#print(dir(o[2]))
#print(o[2].Type)
#print(o[2].X)

if len(o) > 0:
	
	if len(o) == 1:
		print("Select multiple objects (of same type)")
	else:
		#	GET FIRST TYPE
		o_type = o[0].Type
		#print(o_type)
		
		
		if o_type == "DistanceY":
				
			
			a = []
			
			#	ADD FIRST OBJECT
			a.append(o[0])
			#print(a)
			
			#	ADD OTHERS OF SAME TYPE
			for i in range(1, len(o)):
				#print(o[i].ID)
				if o[i].Type == o_type:
					a.append(o[i])
			
			
			#	DUMP
			#print(a)
			for e in a:
				#print("Label: ", e.Label)
				#print("Label2: ", e.Label2)
				print("Name: ", e.Name)
				#print("FullName: ", e.FullName)
				print("ID: ", e.ID)
			
			
			#	ALIGN X
			n = o[0].X
			for i in range(1, len(o)):
				o[i].X = n
			
			
		elif o_type == "DistanceX":
			
			a = []
			
			#	ADD FIRST OBJECT
			a.append(o[0])
			#print(a)
			
			#	ADD OTHERS OF SAME TYPE
			for i in range(1, len(o)):
				#print(o[i].ID)
				if o[i].Type == o_type:
					a.append(o[i])
			
			
			#	DUMP
			#print(a)
			for e in a:
				#print("Label: ", e.Label)
				#print("Label2: ", e.Label2)
				print("Name: ", e.Name)
				#print("FullName: ", e.FullName)
				print("ID: ", e.ID)
			
			
			#	ALIGN Y
			n = o[0].Y
			for i in range(1, len(o)):
				o[i].Y = n
			
		else:
			print("First selected object is not either DistanceY or DistanceX type")

Vadella
Posts: 1
Joined: Sat Dec 03, 2022 2:46 pm

Re: [SOLVED] TechDraw: Align dimensions

Post by Vadella »

Here is a method where you don't have to do the

Code: Select all

o=get_selection()
first.

The first parts gets the selection, checks whether it is not empty and filters it according to the type of the first element selected if needed.

The second part checks which attribute to set and sets it for all selected elements.

Code: Select all

# -*- coding: utf-8 -*-

import FreeCAD
from FreeCAD import Gui

def align(check_type=True):
    selection = Gui.Selection.getCompleteSelection()
    if not selection:
        print("no objects selected")
        return
    first_type = selection[0].Type
    elements = [
        element for element in selection 
        if (not check_type or element.Type == first_type)
    ]
    
    if len(elements) <= 1:
        print("select more than 1 element of the correct type")
        return
        
    if first_type == "DistanceX":
        attribute = "Y"
    else:
        attribute = "X"
        
    default = None
    for element in elements:
        if default is None:
            default = getattr(element, attribute)
            continue
        setattr(element, attribute, default)

align()
You can also copy the method definition in the python console, and then just call

Code: Select all

align()
in the python console after selecting the elements
Post Reply