Scripting the scaling of individual parts of drawing

Need help, or want to share a macro? Post here!
User avatar
quick61
Posts: 3803
Joined: Sat Aug 24, 2013 2:49 am
Location: u.S.A.

Scripting the scaling of individual parts of drawing

Postby quick61 » Fri Nov 08, 2013 9:49 pm

Now that there is a place to put this. (thanks)

Here is a sample file that I have been working with, but any .dxf file that was done in inch (Imperial) scale will do.

https://www.dropbox.com/s/ijhrpdzm4pu90 ... UTFILE.dxf (external link)

What I am wanting to do is develop a script that will scale each line, polyline, spline, arc, circle, etc., individually. Scaling all the lines, arcs, etc. in a group with the Draft, Scale tool leaves a single, un-editable clone. (and don't even get me started on clones :roll: ) I would like to scale this drawing by 25.4, which should result in a properly sized, 1:1 scale drawing in metric units that is intact.

I have been struggling with a couple of things. How to get the script to pick individual objects from the list and then, how to get the script to go to the next object. (for-next in Python)

I have a small script that almost does what I want with a single line, but the result is a "shape" and I'm not so far able to hit on the right syntax to do the Draft.upgrade to convert it to a Wire, though I would really like it if there were some way to convert it to a Line

Code: Select all

# INCH TO METRIC CONVERTER FOR IMPORTED DRAWINGS AND PARTS DONE IN IMPERIAL UNITS
# FreeCAD VERSION 0.14 dev

from PyQt4 import QtCore, QtGui
import FreeCAD, FreeCADGui, Draft

class p():

	def proceed(self):
		try:
			# Line Scaling
			scalefactor=25.4
			inchLine = FreeCAD.ActiveDocument.Line
			matrix=FreeCAD.Matrix()
			matrix.scale(scalefactor,scalefactor,scalefactor)
			metricLine=inchLine.Shape.copy()
			metricLine.transformShape(matrix)
			newMetricLine=FreeCAD.ActiveDocument.addObject('Part::Feature','Line')
			newMetricLine.Shape=metricLine
			inchLine.ViewObject.Visibility=False
			#something here to convert shape to line?
		except:
			FreeCAD.Console.PrintError("UNABLE TO COMPLY")

		self.close()

	def close(self):
		self.dialog.hide()


	def __init__(self):
		self.dialog = None
		self.s1 = None

		# Make dialog box and get input to proceed
		self.dialog = QtGui.QDialog()
		self.dialog.resize(260,100)
		self.dialog.setWindowTitle("Inch to Metric Scale")
		b1 = QtGui.QVBoxLayout(self.dialog)
		t1 = QtGui.QLabel("WARNING:\n Only use once pre drawing!\n Scales drawing by 25.4")
		b1.addWidget(t1)
		okbox = QtGui.QDialogButtonBox(self.dialog)
		okbox.setOrientation(QtCore.Qt.Horizontal)
		okbox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
		b1.addWidget(okbox)
		QtCore.QObject.connect(okbox, QtCore.SIGNAL("accepted()"), self.proceed)
		QtCore.QObject.connect(okbox, QtCore.SIGNAL("rejected()"), self.close)
		QtCore.QMetaObject.connectSlotsByName(self.dialog)
		self.dialog.show()
		self.dialog.exec_()

p()


Are there any scripts out there that would give a good example of how this might be done or any good pointers as to how I could get it to work?

Does any of this make any sense? :?

Oh, and be warned, I am far to new to Python to make much any sense out of it yet. What I have above was hacked together and modified from other peoples work / examples. What I have above only works because I spent lots of time reading error messages in the Report View in FreeCAD, then doctoring it up line by line. ;)

Mark
This post made with 0.0% Micro$oft products - GOT LINUX?
User avatar
yorik
Site Admin
Posts: 11978
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: Scripting the scaling of individual parts of drawing

Postby yorik » Sat Nov 09, 2013 3:05 am

You could use something like this:

Code: Select all

for obj in App.ActiveDocument:
    Draft.scale(App.ActiveDocument.Line,delta=App.Vector(25.4,25.4,25.4),center=App..Vector(0,0,0),legacy=True)
Using legacy=True scales the object itself, instead of creating an evil clone :) Note that it will only work for "editable" draft objects such as lines, polylines, circles, arcs, etc...

*EDIT* hmm seems the new forum looses indentation?
User avatar
sveinki
Posts: 1
Joined: Thu Jun 16, 2016 10:07 am
Location: Glerárþorp, Iceland

Re: Scripting the scaling of individual parts of drawing

Postby sveinki » Thu Jun 16, 2016 10:25 am

Python/scripting newbie here.

I don't manage to run this command, neither in the Python panel nor in a macro. Always getting 'invalid syntax'. Tried all sorts of variants, reading other scripts/macros. Must be missing something.

Code: Select all

from PyQt4 import QtCore,QtGui
import FreeCAD,FreeCADGui,Mesh,Part,MeshPart,Draft

for obj in App.ActiveDocument:
    Draft.scale(App.ActiveDocument.Line,delta=App.Vector(0.01,0.01,0.01),center=App..Vector(0,0,0),legacy=True)
What I'm trying to achieve is to scale a whole document uniformly from, say, centimeters to meters. Of course it would be nice to have a dialog asking for the desired scale value, and possibly this could be conditioned to work on a selection.
User avatar
wandererfan
Posts: 4045
Joined: Tue Nov 06, 2012 5:42 pm

Re: Scripting the scaling of individual parts of drawing

Postby wandererfan » Thu Jun 16, 2016 1:19 pm

Try this:

Code: Select all

newObjs = list()
for obj in App.ActiveDocument.Objects:
    newObjs.append(Draft.scale(obj,delta=App.Vector(2,2,2),center=App.Vector(0,0,0),legacy=False))
#do something with newObjs
It runs. Not too sure about Draft.scale parameters.

wf
mario52
Posts: 3044
Joined: Wed May 16, 2012 2:13 pm

Re: Scripting the scaling of individual parts of drawing

Postby mario52 » Thu Jun 16, 2016 8:19 pm

hi quick61
try this

Code: Select all

# -*- coding: utf-8 -*-
#liste les objets du projet en cours

import os, ImportGui
import FreeCAD
import Draft
doc = FreeCAD.ActiveDocument

objs = FreeCAD.ActiveDocument.Objects
for obj in objs:
    try:
        obj2 = Draft.scale(obj,delta=App.Vector(25.4, 25.4, 25.4),center=App.Vector(0.0,0.0,0.0),copy=True,legacy=True)
    except Exception:
        print "error"
sveinki wrote:Python/scripting newbie here.
error is PyQt4 and replace to PySide
sveinki wrote:What I'm trying to achieve is to scale a whole document uniformly from, say, centimeters to meters. Of course it would be nice to have a dialog asking for the desired scale value, and possibly this could be conditioned to work on a selection.
try this macro Macro_CloneConvert

mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting, Dialog creation. My macros on Gist.github.