Macro FCSpring Helix Variable: Difference between revisions

From FreeCAD Documentation
(create new page "Macro_FCSpring_Helix_Variable" to replace "Macro TruncateSpring")
 
(new presentation and files)
Line 1: Line 1:
{{Template:UnfinishedDocu/fr}}
{{Template:UnfinishedDocu/fr}}


{{Macro|Icon=Text-x-python|Name=FCSpring_Helix_Variable|Description=Creates a spring with helix variable.|Author=Mario52}}
<translate>
<!--T:1-->
{{Macro|Icon=Text-x-python|Name=FCSpring_Helix_Variable|Description=Creates a spring truncate.|Author=Mario52}}


===Description=== <!--T:2-->
===Description===
This macro creates a spring with customizable, any turn may change the spring configuration can be saved in a file with the extension '''.FCSpring'''
This macro create on spring truncate, the truncation is parameterized


<!--T:3-->
[[File:TruncateSpring00.png]]
[[File:TruncateSpring00.png]]
{{clear}}
{{clear}}


===Use=== <!--T:4-->
===Use===
This section is used to configuration the spring
This section is used to configuration the spring


Schematic detail of spring configuration
<!--T:5-->
Schematic detail of spring configuration (corresponds to the basic configuration in the macro)


<!--T:6-->
* '''numberSpires''' : number Spires of spring
* '''rayon''' : radius of spring
* '''pas''' : pas of spire
* '''precision''' : number of points in the circle here '''precision = 5.0''' then 360/5 = 72 points
* '''lastSpire''' : last Spire (high spring)
* '''penultimateSpire''' : penultimate Spire
* '''secondSpire''' : second Spire
* '''firstSpire''' : first spire (low spring)

<!--T:7-->
[[File:Macro TruncateSpring 01.png]]
[[File:Macro TruncateSpring 01.png]]
{{clear}}


[[File:Macro FCSpring Helix Variable 02.png]]
<!--T:8-->
{{clear}}
{{clear}}
[[File:Macro FCSpring Helix Variable 03.png]]
</translate>
{{clear}}
<syntaxhighlight>
[[File:Macro FCSpring Helix Variable 04.png]]
#### Configuration Spring ####################################################
{{clear}}
numberSpires = 10 # numberSpires of spring
[[File:Macro FCSpring Helix Variable 05.png]]
rayon = 20 # radius of spring
{{clear}}
pas = 15 # " ! float " pas of spire
[[File:Macro FCSpring Helix Variable 06.png]]
precision = 5.0 # " ! float " (360/precision) = number points for 1 turn
{{clear}}
#### Configuration 2 + 2 Spires ##############################################
[[File:Macro FCSpring Helix Variable 07.png]]
lastSpire = 0.0 # last Spire (high spring)
{{clear}}
penultimateSpire = pas / 2 # penultimateSpire
[[File:Macro FCSpring Helix Variable 08.png]]
# # normal Spring
{{clear}}
# # normal Spring

secondSpire = pas / 2 # second Spire
firstSpire = 4 # first spire (low spring)
##############################################################################
</syntaxhighlight>


===Script===
<translate>
Macro_FCSpring_Helix_Variable.FCMacro
<!--T:9-->
Note: if you want you can add test


===Script=== <!--T:10-->
</translate>
Macro_TruncateSpring.FCMacro
<syntaxhighlight>
<syntaxhighlight>
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import FreeCAD, FreeCADGui, Draft, Part
"""
***************************************************************************
* Copyright (c) 2014 <mario52> *
* *
* This file is a supplement to the FreeCAD CAx development system. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License (LGPL) *
* as published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* for detail see the LICENCE text file. *
* *
* This software is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this macro; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
* USA *
***************************************************************************
* WARNING! All changes in this file will be lost and *
* may cause malfunction of the program *
***************************************************************************
"""
#
#OS: Windows Vista
#Word size: 32-bit
#Version: 0.14.3700 (Git)
#Branch: releases/FreeCAD-0-14
#Hash: 32f5aae0a64333ec8d5d160dbc46e690510c8fe1
#Python version: 2.6.2
#Qt version: 4.5.2
#Coin version: 3.1.0
#SoQt version: 1.4.1
#
__title__ = "Spring_Helix_Variable"
__author__ = "Mario52"
__url__ = "http://www.freecadweb.org/index-fr.html"
__version__ = "01.02"
__date__ = "17/11/2014"

try:
import PyQt4
from PyQt4 import QtGui ,QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import *
except Exception:
import PySide
from PySide import QtGui ,QtCore
from PySide.QtGui import *
from PySide.QtCore import *
import Draft, Part, FreeCAD, math, PartGui, FreeCADGui, FreeCAD
from math import sqrt, pi, sin, cos, asin
from FreeCAD import Base
from FreeCAD import Base
#
#### Configuration Spring ####################################################
numberSpires = 10 # number Spires of spring
rayon = 20 # radius of spring
pas = 15 # " ! float " pas of spire
precision = 5.0 # " ! float " (360/precision) = number points for 1 turn
#### Configuration 2 + 2 Spires ##############################################
lastSpire = 0.0 # last Spire (high spring)
penultimateSpire = pas / 2 # penultimateSpire
# # normal Spring
# # normal Spring
secondSpire = pas / 2 # second Spire
firstSpire = 4 # first spire (low spring)
##############################################################################


global path
vecligne=[FreeCAD.Vector(0,0,0),FreeCAD.Vector(rayon ,0.0,0.0)] #
path = FreeCAD.ConfigGet("AppHomePath")
ligne = Draft.makeWire(vecligne,closed=False,face=False,support=None) # creation de la ligne de base
#path = FreeCAD.ConfigGet("UserAppData")


global numberSpires ;numberSpires = 10 # number Spires of spring
coor_X = coor_Y = coor_Z = 0.0
global rayon ;rayon = 20.0 # radius of spring
angleTr = 0
global pas ;pas = 15.0 # " ! float " (pas) pitch of spire
points = []
global precision ;precision = 5.0 # " ! float " 360/precision number points for 1 turn
del points[:]
global typeLine ;typeLine = 0 # typeLine 0=BSpline or 1=Wire
ii = 0
global helixS ;helixS = 0.0 # tableau
pas2 = 0.0
global numberSpiresModified;numberSpiresModified = 1 # number (pas) pitch to modify
global pasSpire ;pasSpire = 0.0 # (pas) pitch of spire to modify
global radiusS ;radiusS = 0.0 # radius to modify
global rayonSpire ;rayonSpire = 0.0 #


try:
for spir in range(numberSpires): # number spires Helix
_fromUtf8 = QtCore.QString.fromUtf8
ii += 1
except AttributeError:
if ii == 1: #
def _fromUtf8(s):
pas2 = firstSpire # first spire adjustment
elif ii == 2: #
return s
pas2 = secondSpire # second spire adjustment
elif ii == numberSpires-1: #
pas2 = penultimateSpire # penultimate spire adjustment
elif ii == numberSpires: #
pas2 = lastSpire # last spire adjustment
else:
pas2 = pas


try:
for angleTr in range(0,360,int(precision)): # boucle for 1 turn
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
ligne.Placement = App.Placement(App.Vector(0.0,0.0,0.0), App.Rotation(App.Vector(0,0,1),angleTr), App.Vector(0,0,0))
return QtGui.QApplication.translate(context, text, disambig, _encoding)
a = ligne.Shape.Edges[0].Vertexes[1] # fin de ligne
except AttributeError:
coor_X = (a.Point.x)
def _translate(context, text, disambig):
coor_Y = (a.Point.y)
return QtGui.QApplication.translate(context, text, disambig)
# point = Draft.makePoint(coor_X,coor_Y,coor_Z) # create point repere

points += [FreeCAD.Vector(coor_X,coor_Y,coor_Z)] # coordinates makeBSpline
def errorDialog(msg):
coor_Z += (pas2 / (360/precision)) # pas of spring
diag = QtGui.QMessageBox(QtGui.QMessageBox.Critical,u"Error Message",msg )
try:
diag.setWindowFlags(PyQt4.QtCore.Qt.WindowStaysOnTopHint) # PyQt4 # cette fonction met la fenêtre en avant
except Exception:
diag.setWindowFlags(PySide.QtCore.Qt.WindowStaysOnTopHint) # PySide #cette fonction met la fenêtre en avant
# diag.setWindowModality(QtCore.Qt.ApplicationModal) # la fonction a été désactivée pour favoriser "WindowStaysOnTopHint"
diag.exec_()

class Ui_MainWindow(object):

def setupUi(self, MainWindow):
global path
global numberSpires
global rayon
global pas
global precision
global typeLine
global helixS
global pasSpire
global radiusS

self.window = MainWindow

MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(270, 530)
MainWindow.setMaximumSize(QtCore.QSize(270, 530))
MainWindow.setMinimumSize(QtCore.QSize(270, 530))
self.centralWidget = QtGui.QWidget(MainWindow)
self.centralWidget.setObjectName(_fromUtf8("centralWidget"))

self.groupBox = QtGui.QGroupBox(self.centralWidget)
self.groupBox.setGeometry(QtCore.QRect(10, 40, 251, 201))
self.groupBox.setToolTip(_fromUtf8(""))
self.groupBox.setFlat(False)
self.groupBox.setObjectName(_fromUtf8("groupBox"))

self.DS_Numb_Spires = QtGui.QSpinBox(self.groupBox)
self.DS_Numb_Spires.setGeometry(QtCore.QRect(20, 20, 91, 22))
self.DS_Numb_Spires.setToolTip(_fromUtf8("Number total of coil of the spring"))
self.DS_Numb_Spires.setMaximum(9999999)
self.DS_Numb_Spires.setValue(numberSpires)
self.DS_Numb_Spires.setObjectName(_fromUtf8("DS_Numb_Spires"))
self.DS_Numb_Spires.valueChanged.connect(self.on_DS_Numb_Spires) #connection doubleSpinBox

self.DS_Radius_Sping = QtGui.QDoubleSpinBox(self.groupBox)
self.DS_Radius_Sping.setGeometry(QtCore.QRect(20, 50, 91, 22))
self.DS_Radius_Sping.setToolTip(_fromUtf8("Radius of spring"))
self.DS_Radius_Sping.setDecimals(3)
self.DS_Radius_Sping.setMaximum(9999999.99)
self.DS_Radius_Sping.setValue(rayon)
self.DS_Radius_Sping.setObjectName(_fromUtf8("DS_Radius_Sping"))
self.DS_Radius_Sping.valueChanged.connect(self.on_DS_Radius_Sping) #connection doubleSpinBox

self.DS_Pas_Spring = QtGui.QDoubleSpinBox(self.groupBox)
self.DS_Pas_Spring.setGeometry(QtCore.QRect(20, 80, 91, 22))
self.DS_Pas_Spring.setToolTip(_fromUtf8("Pitch of spring (distance between two vertices)"))
self.DS_Pas_Spring.setDecimals(3)
self.DS_Pas_Spring.setMaximum(9999999.99)
self.DS_Pas_Spring.setValue(pas)
self.DS_Pas_Spring.setObjectName(_fromUtf8("DS_Pas_Spring"))
self.DS_Pas_Spring.valueChanged.connect(self.on_DS_Pas_Spring) #connection doubleSpinBox

self.DS_Precision_Turn = QtGui.QSpinBox(self.groupBox)
self.DS_Precision_Turn.setGeometry(QtCore.QRect(20, 110, 91, 22))
self.DS_Precision_Turn.setToolTip(_fromUtf8("Precision for the line (points = (360/precision))"))
self.DS_Precision_Turn.setMinimum(1)
self.DS_Precision_Turn.setMaximum(360)
self.DS_Precision_Turn.setValue(precision)
self.DS_Precision_Turn.setObjectName(_fromUtf8("DS_Precision_Turn"))
self.DS_Precision_Turn.valueChanged.connect(self.on_DS_Precision_Turn) #connection doubleSpinBox

self.label_2 = QtGui.QLabel(self.groupBox)
self.label_2.setGeometry(QtCore.QRect(120, 20, 111, 16))
self.label_2.setToolTip(_fromUtf8("Number total of coil of the spring"))
self.label_2.setObjectName(_fromUtf8("label_2"))

self.label_3 = QtGui.QLabel(self.groupBox)
self.label_3.setGeometry(QtCore.QRect(120, 50, 111, 16))
self.label_3.setToolTip(_fromUtf8("Radius of spring"))
self.label_3.setText(_fromUtf8("Radius of spring"))
self.label_3.setObjectName(_fromUtf8("label_3"))

self.label_4 = QtGui.QLabel(self.groupBox)
self.label_4.setGeometry(QtCore.QRect(120, 80, 111, 16))
self.label_4.setToolTip(_fromUtf8(""))
self.label_4.setText(_fromUtf8("Pas of spring"))
self.label_4.setObjectName(_fromUtf8("label_4"))

self.label_5 = QtGui.QLabel(self.groupBox)
self.label_5.setGeometry(QtCore.QRect(120, 110, 131, 16))
self.label_5.setToolTip(_fromUtf8(""))
self.label_5.setText(_fromUtf8("Precision of turn"))
self.label_5.setObjectName(_fromUtf8("label_5"))

self.groupBox_3 = QtGui.QGroupBox(self.groupBox)
self.groupBox_3.setGeometry(QtCore.QRect(10, 140, 231, 51))
self.groupBox_3.setObjectName(_fromUtf8("groupBox_3"))

self.RA_Wire = QtGui.QRadioButton(self.groupBox_3)
self.RA_Wire.setGeometry(QtCore.QRect(120, 20, 95, 20))
self.RA_Wire.setToolTip(_fromUtf8("Type line BSpline"))
self.RA_Wire.setText(_fromUtf8("Wire"))
# self.RA_Wire.setChecked(True)
self.RA_Wire.setObjectName(_fromUtf8("RA_Wire"))
self.RA_Wire.clicked.connect(self.on_RA_Wire) #connection radioButton

self.RA_BSpline = QtGui.QRadioButton(self.groupBox_3)
self.RA_BSpline.setGeometry(QtCore.QRect(10, 20, 95, 20))
self.RA_BSpline.setToolTip(_fromUtf8("Type line BSpline"))
self.RA_BSpline.setText(_fromUtf8("BSpline"))
self.RA_BSpline.setChecked(True)
self.RA_BSpline.setObjectName(_fromUtf8("RA_BSpline"))
self.RA_BSpline.clicked.connect(self.on_RA_BSpline) #connection radioButton

self.groupBox_2 = QtGui.QGroupBox(self.centralWidget)
self.groupBox_2.setGeometry(QtCore.QRect(10, 250, 251, 201))
self.groupBox_2.setToolTip(_fromUtf8("This section allows you to adjust the distance from the coil named. EX: 1 spire = 2 mm"))
self.groupBox_2.setCheckable(False)
self.groupBox_2.setChecked(False)
self.groupBox_2.setObjectName(_fromUtf8("groupBox_2"))

self.S_Numbering_Spire = QtGui.QSpinBox(self.groupBox_2)
self.S_Numbering_Spire.setGeometry(QtCore.QRect(20, 20, 91, 22))
self.S_Numbering_Spire.setToolTip(_fromUtf8("Numbering of coil for 1 to max = Number of coil ("+str(numberSpires)+")"))
self.S_Numbering_Spire.setMinimum(1)
self.S_Numbering_Spire.setMaximum(numberSpires)
self.S_Numbering_Spire.setObjectName(_fromUtf8("S_Nmbering_Spire"))
self.S_Numbering_Spire.valueChanged.connect(self.on_S_Numbering_Spire) #connection SpinBox

self.DS_Pas_Spire = QtGui.QDoubleSpinBox(self.groupBox_2)
self.DS_Pas_Spire.setGeometry(QtCore.QRect(20, 50, 91, 22))
self.DS_Pas_Spire.setToolTip(_fromUtf8("Pitch of the coil (The pitch is the distance betwen 2 vertices)"))
self.DS_Pas_Spire.setDecimals(3)
self.DS_Pas_Spire.setMaximum(9999999.99)
self.DS_Pas_Spire.setObjectName(_fromUtf8("DS_Pas_Spire"))
self.DS_Pas_Spire.valueChanged.connect(self.on_DS_Pas_Spire) #connection doubleSpinBox

self.label_6 = QtGui.QLabel(self.groupBox_2)
self.label_6.setGeometry(QtCore.QRect(120, 20, 121, 16))
self.label_6.setToolTip(_fromUtf8("Numbering of coil for 1 to max = Number of coil"))
self.label_6.setText(_fromUtf8("Numbering of coil"))
self.label_6.setObjectName(_fromUtf8("label_6"))

self.label_7 = QtGui.QLabel(self.groupBox_2)
self.label_7.setGeometry(QtCore.QRect(120, 50, 121, 16))
self.label_7.setToolTip(_fromUtf8("Pitch of the coil (The pitch is the distance betwen 2 vertices)"))
self.label_7.setObjectName(_fromUtf8("label_7"))

self.PU_Accept_Value = QtGui.QPushButton(self.groupBox_2)
self.PU_Accept_Value.setGeometry(QtCore.QRect(20, 110, 211, 21))
self.PU_Accept_Value.setToolTip(_fromUtf8("Accept the value for the coil "))
self.PU_Accept_Value.setObjectName(_fromUtf8("PU_Accept_Value"))
self.PU_Accept_Value.clicked.connect(self.on_PU_Accept_Value)

self.textEdit = QtGui.QTextEdit(self.groupBox_2)
self.textEdit.setGeometry(QtCore.QRect(20, 140, 211, 55))
self.textEdit.setToolTip(_fromUtf8("List alls modification of coil "))
self.textEdit.setObjectName(_fromUtf8("textEdit"))
self.textEdit.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse)

# self.DS_Radius_Spire = QtGui.QDoubleSpinBox(self.groupBox_2)
# self.DS_Radius_Spire.setGeometry(QtCore.QRect(20, 80, 91, 22))
# self.DS_Radius_Spire.setToolTip(_fromUtf8("Radiuf of coil "))
# self.DS_Radius_Spire.setDecimals(3)
# self.DS_Radius_Spire.setMaximum(9999999.99)
# self.DS_Radius_Spire.setObjectName(_fromUtf8("DS_Radius_Spire"))

# self.label_8 = QtGui.QLabel(self.groupBox_2)
# self.label_8.setGeometry(QtCore.QRect(120, 80, 121, 16))
# self.label_8.setToolTip(_fromUtf8("Radiuf of coil "))
# self.label_8.setText(_fromUtf8("Radius of coil"))
# self.label_8.setObjectName(_fromUtf8("label_8"))

self.PU_Quit = QtGui.QPushButton(self.centralWidget)
self.PU_Quit.setGeometry(QtCore.QRect(10, 490, 81, 28))
self.PU_Quit.setToolTip(_fromUtf8("Quit the macro"))
self.PU_Quit.setText(_fromUtf8("Quit"))
self.PU_Quit.setObjectName(_fromUtf8("PU_Quit"))
self.PU_Quit.clicked.connect(self.on_PU_Quit)

self.PU_Launch = QtGui.QPushButton(self.centralWidget)
self.PU_Launch.setGeometry(QtCore.QRect(170, 490, 93, 28))
self.PU_Launch.setToolTip(_fromUtf8("launch the macro and create the spring"))
self.PU_Launch.setText(_fromUtf8("Launch "))
self.PU_Launch.setObjectName(_fromUtf8("PU_Launch"))
self.PU_Launch.clicked.connect(self.on_PU_Launch)

self.label = QtGui.QLabel(self.centralWidget)
self.label.setGeometry(QtCore.QRect(23, 0, 280, 31))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setItalic(True)
font.setUnderline(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setText(_fromUtf8(" Spring Helix Variable "))
self.label.setObjectName(_fromUtf8("label"))
MainWindow.setCentralWidget(self.centralWidget)

self.PU_Reset = QtGui.QPushButton(self.centralWidget)
self.PU_Reset.setGeometry(QtCore.QRect(100, 490, 61, 28))
self.PU_Reset.setToolTip(_fromUtf8("Reset the data "))
self.PU_Reset.setObjectName(_fromUtf8("PU_Reset"))
self.PU_Reset.clicked.connect(self.on_PU_Reset)

self.PU_Read = QtGui.QPushButton(self.centralWidget)
self.PU_Read.setGeometry(QtCore.QRect(10, 460, 121, 28))
self.PU_Read.setToolTip(_fromUtf8("Read the file "))
self.PU_Read.setText(_fromUtf8("Read"))
self.PU_Read.setObjectName(_fromUtf8("on_PU_Read"))
self.PU_Read.clicked.connect(self.on_PU_Read)

self.PU_Save = QtGui.QPushButton(self.centralWidget)
self.PU_Save.setGeometry(QtCore.QRect(140, 460, 121, 28))
self.PU_Save.setToolTip(_fromUtf8("Save the file"))
self.PU_Save.setText(_fromUtf8("Save"))
self.PU_Save.setObjectName(_fromUtf8("on_PU_Save"))
self.PU_Save.clicked.connect(self.on_PU_Save)

MainWindow.setCentralWidget(self.centralWidget)

self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
try:
MainWindow.setWindowFlags(PyQt4.QtCore.Qt.WindowStaysOnTopHint) # PyQt4 cette fonction met la fenêtre en avant
except Exception:
MainWindow.setWindowFlags(PySide.QtCore.Qt.WindowStaysOnTopHint) # PySide cette fonction met la fenêtre en avant

MainWindow.setWindowTitle("Spring Helix Variable")
self.groupBox.setTitle("Configure")
self.label_2.setText("Number of coil")
self.groupBox_3.setTitle("Type line BSpline")
self.RA_Wire.setToolTip("Type line Wire")
self.groupBox_2.setTitle( "Coil special length")
self.label_7.setText("Pitch of coil")
self.PU_Accept_Value.setText("Accept the value modified")
self.textEdit.setText("")
self.PU_Reset.setText("Reset")

def on_DS_Numb_Spires(self,value): # nombre de spire total
global pas
global numberSpires
global helixS
global radiusS
numberSpires = value
helixS = []
del helixS[:]
helixS = numberSpires*[pas]
radiusS = []
del radiusS[:]
radiusS = numberSpires*[rayon]
self.S_Numbering_Spire.setToolTip(_fromUtf8("Numbering of coil for 1 to max = Number of spires ("+str(numberSpires)+")"))
# App.Console.PrintMessage(str("on_DS_Numb_Spires ")+str(numberSpires)+"\n")

def on_DS_Radius_Sping(self,value): # rayon axial du ressort
global rayon
rayon = value
# App.Console.PrintMessage(str("on_DS_Radius_Sping ")+str(rayon)+"\n")

def on_DS_Pas_Spring(self,value): # pas (pitch) du ressort
global pas
global numberSpires
global helixS
global numberSpiresModified
global pasSpire
global radiusS
pas = value
numberSpiresModified = 0
self.S_Numbering_Spire.setValue(numberSpiresModified)
pasSpire = 0.0
self.DS_Pas_Spire.setValue(pasSpire)
helixS = []
del helixS[:]
helixS = numberSpires*[pas]
radiusS = []
del radiusS[:]
radiusS = numberSpires*[rayon]
# App.Console.PrintMessage(str("on_DS_Pas_Spring ")+str(pas)+"\n")

def on_DS_Precision_Turn(self,value): # " ! float " 360/precision number points for 1 turn
global precision
precision = value
self.label_5.setText(_fromUtf8("Precision = " + str(360/precision) + " points"))
# App.Console.PrintMessage(str("on_DS_Precision_Turn ")+str(precision)+"\n")

def on_RA_Wire(self): #
global typeLine
typeLine = 1
# App.Console.PrintMessage(str("on_RA_Wire ")+str(typeLine)+"\n")

def on_RA_BSpline(self): #
global typeLine
typeLine = 0
# App.Console.PrintMessage(str("on_RA_BSpline ")+str(typeLine)+"\n")

def on_S_Numbering_Spire(self,value): # numero de la spire a modifier
global numberSpires
global numberSpiresModified
self.S_Numbering_Spire.setMaximum(numberSpires)
numberSpiresModified = value
# App.Console.PrintMessage(str("on_S_Nubering_Spire ")+str(numberSpiresModified)+"\n")

def on_DS_Pas_Spire(self,value): # pas (pitch) de la spire a modifier
global pasSpire
pasSpire = value
# App.Console.PrintMessage(str("on_DS_Pas_Spire ")+str(pasSpire)+"\n")

def on_PU_Accept_Value(self): # accepter la modification de la spire
global numberSpiresModified
global pasSpire
global rayonSpire
global helixS
global radiusS
if numberSpiresModified == 1:
helixS[0] = pasSpire
radiusS[0]= rayonSpire
self.textEdit.append("Coil number " + str(numberSpiresModified) + " = " + str(helixS[0]) + " " + str(radiusS[0]))
App.Console.PrintMessage("Coil number " + str(numberSpiresModified) + " = " + str(helixS[0]) + " " + str(radiusS[0])+"\n")
else:
helixS[numberSpiresModified-1] = pasSpire
radiusS[numberSpiresModified-1]= rayonSpire
self.textEdit.append("Coil number " + str(numberSpiresModified) + " = " + str(helixS[numberSpiresModified-1]) + " " + str(radiusS[numberSpiresModified-1]))
App.Console.PrintMessage("Coil number " + str(numberSpiresModified) + " = " + str(helixS[numberSpiresModified-1]) + " " + str(radiusS[numberSpiresModified-1])+"\n")
# App.Console.PrintMessage(str("on_PU_Accept_Value ")+"\n")

def on_PU_Quit(self): # Quit
App.Console.PrintMessage("\n"+"Fin FCSpring_Helix_Variable"+"\n"+"___________________________"+"\n")
self.window.hide()
# App.Console.PrintMessage(str(" ")+str()+"\n")

def on_PU_Reset(self): # Reset
global numberSpires
global rayon
global pas
global precision
global typeLine
global helixS
global numberSpiresModified
global pasSpire
global radiusS
numberSpires = 10
rayon = 20.0
pas = 15.0
precision = 5.0
typeLine = 0
helixS = 0.0
numberSpiresModified = 1
pasSpire = 0.0

self.DS_Numb_Spires.setValue(numberSpires)
self.DS_Radius_Sping.setValue(rayon)
self.DS_Pas_Spring.setValue(pas)
self.DS_Precision_Turn.setValue(precision)
self.RA_BSpline.setChecked(True)
self.S_Numbering_Spire.setValue(numberSpiresModified)
self.DS_Pas_Spire.setValue(pasSpire)
self.textEdit.setText("")
self.textEdit.clear()
helixS = []
del helixS[:]
helixS = numberSpires*[pas]
radiusS = []
del radiusS[:]
radiusS = numberSpires*[rayon]
# App.Console.PrintMessage(str("on_PU_Reset ")+"\n")

def on_PU_Read(self): # lecture
global path
global numberSpires
global rayon
global pas
global precision
global typeLine
global helixS
global radiusS

OpenName = ""
try:
OpenName = QFileDialog.getOpenFileName(None,QString.fromLocal8Bit("Read a file FCSpring"),path,"*.FCSpring") # PyQt4
except Exception:
OpenName, Filter = PySide.QtGui.QFileDialog.getOpenFileName(None, "Read a file FCSpring", path, "*.FCSpring")#PySide
try:
if OpenName != "":
try:
helixS = []
radiusS = []
del helixS[:]
del radiusS[:]
file = open(OpenName, "r") #
Header = file.readline().rstrip('\n\r')
if Header == "FCString":
self.textEdit.clear()
App.Console.PrintMessage(str(Header)+"\n")
numberSpires = file.readline().rstrip('\n\r')
numberSpires = int(numberSpires)
self.DS_Numb_Spires.setValue(numberSpires)
App.Console.PrintMessage(str(numberSpires)+"\n")

rayon = file.readline().rstrip('\n\r')
rayon = float(rayon)
radiusS = numberSpires*[rayon]
self.DS_Radius_Sping.setValue(rayon)
App.Console.PrintMessage(str(rayon)+"\n")

pas = file.readline().rstrip('\n\r')
pas = float(pas)
helixS = numberSpires*[pas]
self.DS_Pas_Spring.setValue(pas)
App.Console.PrintMessage(str(pas)+"\n")

precision = file.readline().rstrip('\n\r')
precision = float(precision)
self.DS_Precision_Turn.setValue(precision)
App.Console.PrintMessage(str(precision)+"\n")

typeLine = file.readline().rstrip('\n\r') # typeLine 0=BSpline or 1=Wire
typeLine = int(typeLine)
if typeLine == 0:
self.RA_BSpline.setChecked(True)
else:
self.RA_Wire.setChecked(True)

dummy = file.readline().rstrip('\n\r')
i = 0
for ligne in file:
a , b = ligne.rstrip('\n\r').split(" ")
helixS[i] = float(a)
radiusS[i] = float(b)
App.Console.PrintMessage(str(i+1)+" "+str(helixS[i])+" "+str(radiusS[i])+"\n")
self.textEdit.append("Coil number " + str(i+1) + " = " + str(helixS[i]) + " " + str(radiusS[i]))
i += 1
else:
App.Console.PrintMessage("Error file not FCSpring "+"\n")
errorDialog("Error file not FCSpring "+OpenName)
finally:
file.close()
except Exception:
App.Console.PrintMessage("Error in reading the file "+OpenName+"\n")
errorDialog("Error in reading the file "+OpenName)

def on_PU_Save(self): # enregistrement
global path
global numberSpires
global rayon
global pas
global precision
global typeLine
global helixS
global radiusS

SaveName = ""
try:
SaveName = QFileDialog.getSaveFileName(None,QString.fromLocal8Bit("Save a file FCSpring"),path,"*.FCSpring") # PyQt4
except Exception:
SaveName, Filter = PySide.QtGui.QFileDialog.getSaveFileName(None, "Save a file FCSpring", path, "*.FCSpring") # PySide
if SaveName == "":
App.Console.PrintMessage("Process aborted"+"\n")
errorDialog("Process aborted")
else:
App.Console.PrintMessage("Registration of "+SaveName+"\n")
try:
file = open(SaveName, 'w') # write
try:
file.write("FCString"+"\n")
file.write(str(numberSpires)+"\n")
file.write(str(rayon)+"\n")
file.write(str(pas)+"\n")
file.write(str(precision)+"\n")
file.write(str(typeLine)+"\n")
file.write("\n")
for i in range(numberSpires):
file.write(str(helixS[i])+" "+str(radiusS[i])+"\n")
finally:
file.close()
except Exception:
App.Console.PrintMessage("Error Registration file "+SaveName+"\n")
errorDialog("Error Registration file "+SaveName)

def on_PU_Launch(self): # Execute
global numberSpires
global rayon
global pas
global precision
global typeLine
global helixS
global pasSpire
global radiusS

doc = FreeCAD.ActiveDocument
if doc == None:
doc = FreeCAD.newDocument()

vecligne=[FreeCAD.Vector(0,0,0),FreeCAD.Vector(rayon ,0.0,0.0)]
ligne = Draft.makeWire(vecligne,closed=False,face=False,support=None) #creation de la ligne de base
coor_X = coor_Y = coor_Z = 0.0
angleTr = 0
points = []
del points[:]
pas2 = 0.0
App.Console.PrintMessage("List " + str(helixS)+"\n")
App.Console.PrintMessage("___________________________"+"\n")
for spir in range(numberSpires): # number spires Helix
pas2 = helixS[spir]
for angleTr in range(0,360,int(precision)): #boucle for 1 turn
ligne.Placement = FreeCAD.ActiveDocument.Line.Placement=App.Placement(App.Vector(0.0,0.0,0.0), App.Rotation(App.Vector(0,0,1),angleTr), App.Vector(0,0,0))
a = ligne.Shape.Edges[0].Vertexes[1] #fin de ligne
coor_X = (a.Point.x)
coor_Y = (a.Point.y)
points += [FreeCAD.Vector(coor_X,coor_Y,coor_Z)] # coordinates makeBSpline
coor_Z += (pas2 / (360/precision)) # pas of spring
# point = Draft.makePoint(coor_X,coor_Y,coor_Z) # create point repere for test
App.ActiveDocument.removeObject(ligne.Name) # remove ligne de base directrice

if typeLine == 1:
ressort = Draft.makeWire(points,closed=False,face=False,support=None)# creation spring makeWire
else:
ressort = Draft.makeBSpline(points,closed=False) # creation spring makeBSpline
FreeCAD.ActiveDocument.recompute()
helixS = []
del helixS[:]
helixS = numberSpires*[pas]
radiusS = []
del radiusS[:]
radiusS = numberSpires*[rayon]

MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()


App.ActiveDocument.removeObject(ligne.Name) # remove ligne de base directrice
#ressort = Draft.makeWire(points,closed=False,face=False,support=None) # creation spring makeWire
ressort = Draft.makeBSpline(points,closed=False) # creation spring makeBSpline
FreeCAD.ActiveDocument.recompute()
</syntaxhighlight>
</syntaxhighlight>


===Links===
<translate>
===Links=== <!--T:11-->
The forum discussion [http://forum.freecadweb.org/viewtopic.php?f=3&t=8313&p=68161#p68161 Try to do a Spring]
The forum discussion [http://forum.freecadweb.org/viewtopic.php?f=3&t=8313&p=68161#p68161 Try to do a Spring]


===Version=== <!--T:12-->
===Project===
modify diameter to any coil

===Version===
17/11/2014: ver 1.2 : new version with GUI and modification any coil and save or load the data to disk.

10/11/2014: (23h20) correction of the modification
10/11/2014: (23h20) correction of the modification
<syntaxhighlight>
<syntaxhighlight>
Line 133: Line 687:
</syntaxhighlight>
</syntaxhighlight>
6/11/2014 : adding "makeBSpline" and configuration
6/11/2014 : adding "makeBSpline" and configuration
{{clear}}


===Limitations===
</translate>

[[File:Macro FCSpring Helix Variable 09.png]]
{{clear}}
[[File:Macro FCSpring Helix Variable 10.png]]
{{clear}}
[[File:Macro FCSpring Helix Variable 11.png]]
{{clear}}
{{clear}}
<languages/>

Revision as of 20:03, 18 November 2014

Cette documentation n'est pas terminée. Merci de nous aider et de contribuer à la documentation.

Modèle GuiCommand explique comment les commandes doivent être documentées. Parcourez Category:UnfinishedDocu pour voir d'autres pages incomplètes comme celle-ci. Voir Category:Command_Reference pour toutes les commandes.

Voir Écrire une page Wiki pour apprendre à éditer les pages du wiki, et aller à Contribuer à FreeCAD pour apprendre d'autres façons de contribuer.

File:Text-x-python FCSpring_Helix_Variable

Description
Creates a spring with helix variable.

Author: Mario52
Author
Mario52
Download
None
Links
Macro Version
1.0
Date last modified
None
FreeCAD Version(s)
None
Default shortcut
None
See also
None

Description

This macro creates a spring with customizable, any turn may change the spring configuration can be saved in a file with the extension .FCSpring

Use

This section is used to configuration the spring

Schematic detail of spring configuration


Script

Macro_FCSpring_Helix_Variable.FCMacro

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
"""
***************************************************************************
*   Copyright (c) 2014 <mario52>                                          *
*                                                                         *
*   This file is a supplement to the FreeCAD CAx development system.      *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU Lesser General Public License (LGPL)    *
*   as published by the Free Software Foundation; either version 2 of     *
*   the License, or (at your option) any later version.                   *
*   for detail see the LICENCE text file.                                 *
*                                                                         *
*   This software is distributed in the hope that it will be useful,      *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU Library General Public License for more details.                  *
*                                                                         *
*   You should have received a copy of the GNU Library General Public     *
*   License along with this macro; if not, write to the Free Software     *
*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
*   USA                                                                   *
***************************************************************************
*           WARNING! All changes in this file will be lost and            *  
*                  may cause malfunction of the program                   *
***************************************************************************
"""
#
#OS: Windows Vista
#Word size: 32-bit
#Version: 0.14.3700 (Git)
#Branch: releases/FreeCAD-0-14
#Hash: 32f5aae0a64333ec8d5d160dbc46e690510c8fe1
#Python version: 2.6.2
#Qt version: 4.5.2
#Coin version: 3.1.0
#SoQt version: 1.4.1
#
__title__   = "Spring_Helix_Variable"
__author__  = "Mario52"
__url__     = "http://www.freecadweb.org/index-fr.html"
__version__ = "01.02"
__date__    = "17/11/2014"

try:
    import PyQt4
    from PyQt4 import QtGui ,QtCore
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *
except Exception:
    import PySide
    from PySide import QtGui ,QtCore
    from PySide.QtGui import *
    from PySide.QtCore import *
 
import Draft, Part, FreeCAD, math, PartGui, FreeCADGui, FreeCAD
from math import sqrt, pi, sin, cos, asin
from FreeCAD import Base

global path
path = FreeCAD.ConfigGet("AppHomePath")
#path = FreeCAD.ConfigGet("UserAppData")

global numberSpires        ;numberSpires         = 10     # number Spires of spring
global rayon               ;rayon                = 20.0   # radius of  spring
global pas                 ;pas                  = 15.0   # " ! float " (pas) pitch of spire
global precision           ;precision            = 5.0    # " ! float " 360/precision number points for 1 turn
global typeLine            ;typeLine             = 0      # typeLine 0=BSpline or 1=Wire
global helixS              ;helixS               = 0.0    # tableau
global numberSpiresModified;numberSpiresModified = 1      # number (pas) pitch to modify
global pasSpire            ;pasSpire             = 0.0    # (pas) pitch of spire to modify
global radiusS             ;radiusS              = 0.0    # radius to modify
global rayonSpire          ;rayonSpire           = 0.0    # 

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

def errorDialog(msg):
    diag = QtGui.QMessageBox(QtGui.QMessageBox.Critical,u"Error Message",msg )
    try:
        diag.setWindowFlags(PyQt4.QtCore.Qt.WindowStaysOnTopHint) # PyQt4 # cette fonction met la fenêtre en avant
    except Exception:    
        diag.setWindowFlags(PySide.QtCore.Qt.WindowStaysOnTopHint) # PySide #cette fonction met la fenêtre en avant
#    diag.setWindowModality(QtCore.Qt.ApplicationModal)       # la fonction a été désactivée pour favoriser "WindowStaysOnTopHint"
    diag.exec_()

class Ui_MainWindow(object):

    def setupUi(self, MainWindow):
        global path
        global numberSpires
        global rayon
        global pas
        global precision
        global typeLine
        global helixS
        global pasSpire
        global radiusS

        self.window = MainWindow

        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(270, 530)
        MainWindow.setMaximumSize(QtCore.QSize(270, 530))
        MainWindow.setMinimumSize(QtCore.QSize(270, 530))
        self.centralWidget = QtGui.QWidget(MainWindow)
        self.centralWidget.setObjectName(_fromUtf8("centralWidget"))

        self.groupBox = QtGui.QGroupBox(self.centralWidget)
        self.groupBox.setGeometry(QtCore.QRect(10, 40, 251, 201))
        self.groupBox.setToolTip(_fromUtf8(""))
        self.groupBox.setFlat(False)
        self.groupBox.setObjectName(_fromUtf8("groupBox"))

        self.DS_Numb_Spires = QtGui.QSpinBox(self.groupBox)
        self.DS_Numb_Spires.setGeometry(QtCore.QRect(20, 20, 91, 22))
        self.DS_Numb_Spires.setToolTip(_fromUtf8("Number total of coil of the spring"))
        self.DS_Numb_Spires.setMaximum(9999999)
        self.DS_Numb_Spires.setValue(numberSpires)
        self.DS_Numb_Spires.setObjectName(_fromUtf8("DS_Numb_Spires"))
        self.DS_Numb_Spires.valueChanged.connect(self.on_DS_Numb_Spires) #connection doubleSpinBox

        self.DS_Radius_Sping = QtGui.QDoubleSpinBox(self.groupBox)
        self.DS_Radius_Sping.setGeometry(QtCore.QRect(20, 50, 91, 22))
        self.DS_Radius_Sping.setToolTip(_fromUtf8("Radius of spring"))
        self.DS_Radius_Sping.setDecimals(3)
        self.DS_Radius_Sping.setMaximum(9999999.99)
        self.DS_Radius_Sping.setValue(rayon)
        self.DS_Radius_Sping.setObjectName(_fromUtf8("DS_Radius_Sping"))
        self.DS_Radius_Sping.valueChanged.connect(self.on_DS_Radius_Sping) #connection doubleSpinBox

        self.DS_Pas_Spring = QtGui.QDoubleSpinBox(self.groupBox)
        self.DS_Pas_Spring.setGeometry(QtCore.QRect(20, 80, 91, 22))
        self.DS_Pas_Spring.setToolTip(_fromUtf8("Pitch of spring (distance between two vertices)"))
        self.DS_Pas_Spring.setDecimals(3)
        self.DS_Pas_Spring.setMaximum(9999999.99)
        self.DS_Pas_Spring.setValue(pas)
        self.DS_Pas_Spring.setObjectName(_fromUtf8("DS_Pas_Spring"))
        self.DS_Pas_Spring.valueChanged.connect(self.on_DS_Pas_Spring) #connection doubleSpinBox

        self.DS_Precision_Turn = QtGui.QSpinBox(self.groupBox)
        self.DS_Precision_Turn.setGeometry(QtCore.QRect(20, 110, 91, 22))
        self.DS_Precision_Turn.setToolTip(_fromUtf8("Precision for the line (points = (360/precision))"))
        self.DS_Precision_Turn.setMinimum(1)
        self.DS_Precision_Turn.setMaximum(360)
        self.DS_Precision_Turn.setValue(precision)
        self.DS_Precision_Turn.setObjectName(_fromUtf8("DS_Precision_Turn"))
        self.DS_Precision_Turn.valueChanged.connect(self.on_DS_Precision_Turn) #connection doubleSpinBox

        self.label_2 = QtGui.QLabel(self.groupBox)
        self.label_2.setGeometry(QtCore.QRect(120, 20, 111, 16))
        self.label_2.setToolTip(_fromUtf8("Number total of coil of the spring"))
        self.label_2.setObjectName(_fromUtf8("label_2"))

        self.label_3 = QtGui.QLabel(self.groupBox)
        self.label_3.setGeometry(QtCore.QRect(120, 50, 111, 16))
        self.label_3.setToolTip(_fromUtf8("Radius of spring"))
        self.label_3.setText(_fromUtf8("Radius of spring"))
        self.label_3.setObjectName(_fromUtf8("label_3"))

        self.label_4 = QtGui.QLabel(self.groupBox)
        self.label_4.setGeometry(QtCore.QRect(120, 80, 111, 16))
        self.label_4.setToolTip(_fromUtf8(""))
        self.label_4.setText(_fromUtf8("Pas of spring"))
        self.label_4.setObjectName(_fromUtf8("label_4"))

        self.label_5 = QtGui.QLabel(self.groupBox)
        self.label_5.setGeometry(QtCore.QRect(120, 110, 131, 16))
        self.label_5.setToolTip(_fromUtf8(""))
        self.label_5.setText(_fromUtf8("Precision of turn"))
        self.label_5.setObjectName(_fromUtf8("label_5"))

        self.groupBox_3 = QtGui.QGroupBox(self.groupBox)
        self.groupBox_3.setGeometry(QtCore.QRect(10, 140, 231, 51))
        self.groupBox_3.setObjectName(_fromUtf8("groupBox_3"))

        self.RA_Wire = QtGui.QRadioButton(self.groupBox_3)
        self.RA_Wire.setGeometry(QtCore.QRect(120, 20, 95, 20))
        self.RA_Wire.setToolTip(_fromUtf8("Type line BSpline"))
        self.RA_Wire.setText(_fromUtf8("Wire"))
#        self.RA_Wire.setChecked(True)
        self.RA_Wire.setObjectName(_fromUtf8("RA_Wire"))
        self.RA_Wire.clicked.connect(self.on_RA_Wire) #connection radioButton

        self.RA_BSpline = QtGui.QRadioButton(self.groupBox_3)
        self.RA_BSpline.setGeometry(QtCore.QRect(10, 20, 95, 20))
        self.RA_BSpline.setToolTip(_fromUtf8("Type line BSpline"))
        self.RA_BSpline.setText(_fromUtf8("BSpline"))
        self.RA_BSpline.setChecked(True)
        self.RA_BSpline.setObjectName(_fromUtf8("RA_BSpline"))
        self.RA_BSpline.clicked.connect(self.on_RA_BSpline) #connection radioButton

        self.groupBox_2 = QtGui.QGroupBox(self.centralWidget)
        self.groupBox_2.setGeometry(QtCore.QRect(10, 250, 251, 201))
        self.groupBox_2.setToolTip(_fromUtf8("This section allows you to adjust the distance from the coil named. EX: 1 spire = 2 mm"))
        self.groupBox_2.setCheckable(False)
        self.groupBox_2.setChecked(False)
        self.groupBox_2.setObjectName(_fromUtf8("groupBox_2"))

        self.S_Numbering_Spire = QtGui.QSpinBox(self.groupBox_2)
        self.S_Numbering_Spire.setGeometry(QtCore.QRect(20, 20, 91, 22))
        self.S_Numbering_Spire.setToolTip(_fromUtf8("Numbering of coil for 1 to max = Number of coil ("+str(numberSpires)+")"))
        self.S_Numbering_Spire.setMinimum(1)
        self.S_Numbering_Spire.setMaximum(numberSpires)
        self.S_Numbering_Spire.setObjectName(_fromUtf8("S_Nmbering_Spire"))
        self.S_Numbering_Spire.valueChanged.connect(self.on_S_Numbering_Spire) #connection SpinBox

        self.DS_Pas_Spire = QtGui.QDoubleSpinBox(self.groupBox_2)
        self.DS_Pas_Spire.setGeometry(QtCore.QRect(20, 50, 91, 22))
        self.DS_Pas_Spire.setToolTip(_fromUtf8("Pitch of the coil (The pitch is the distance betwen 2 vertices)"))
        self.DS_Pas_Spire.setDecimals(3)
        self.DS_Pas_Spire.setMaximum(9999999.99)
        self.DS_Pas_Spire.setObjectName(_fromUtf8("DS_Pas_Spire"))
        self.DS_Pas_Spire.valueChanged.connect(self.on_DS_Pas_Spire) #connection doubleSpinBox

        self.label_6 = QtGui.QLabel(self.groupBox_2)
        self.label_6.setGeometry(QtCore.QRect(120, 20, 121, 16))
        self.label_6.setToolTip(_fromUtf8("Numbering of coil for 1 to max =  Number of coil"))
        self.label_6.setText(_fromUtf8("Numbering of coil"))
        self.label_6.setObjectName(_fromUtf8("label_6"))

        self.label_7 = QtGui.QLabel(self.groupBox_2)
        self.label_7.setGeometry(QtCore.QRect(120, 50, 121, 16))
        self.label_7.setToolTip(_fromUtf8("Pitch of the coil (The pitch is the distance betwen 2 vertices)"))
        self.label_7.setObjectName(_fromUtf8("label_7"))

        self.PU_Accept_Value = QtGui.QPushButton(self.groupBox_2)
        self.PU_Accept_Value.setGeometry(QtCore.QRect(20, 110, 211, 21))
        self.PU_Accept_Value.setToolTip(_fromUtf8("Accept the value for the coil "))
        self.PU_Accept_Value.setObjectName(_fromUtf8("PU_Accept_Value"))
        self.PU_Accept_Value.clicked.connect(self.on_PU_Accept_Value)

        self.textEdit = QtGui.QTextEdit(self.groupBox_2)
        self.textEdit.setGeometry(QtCore.QRect(20, 140, 211, 55))
        self.textEdit.setToolTip(_fromUtf8("List alls modification of coil "))
        self.textEdit.setObjectName(_fromUtf8("textEdit"))
        self.textEdit.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse)

#        self.DS_Radius_Spire = QtGui.QDoubleSpinBox(self.groupBox_2)
#        self.DS_Radius_Spire.setGeometry(QtCore.QRect(20, 80, 91, 22))
#        self.DS_Radius_Spire.setToolTip(_fromUtf8("Radiuf of coil "))
#        self.DS_Radius_Spire.setDecimals(3)
#        self.DS_Radius_Spire.setMaximum(9999999.99)
#        self.DS_Radius_Spire.setObjectName(_fromUtf8("DS_Radius_Spire"))

#        self.label_8 = QtGui.QLabel(self.groupBox_2)
#        self.label_8.setGeometry(QtCore.QRect(120, 80, 121, 16))
#        self.label_8.setToolTip(_fromUtf8("Radiuf of coil "))
#        self.label_8.setText(_fromUtf8("Radius of coil"))
#        self.label_8.setObjectName(_fromUtf8("label_8"))

        self.PU_Quit = QtGui.QPushButton(self.centralWidget)
        self.PU_Quit.setGeometry(QtCore.QRect(10, 490, 81, 28))
        self.PU_Quit.setToolTip(_fromUtf8("Quit the macro"))
        self.PU_Quit.setText(_fromUtf8("Quit"))
        self.PU_Quit.setObjectName(_fromUtf8("PU_Quit"))
        self.PU_Quit.clicked.connect(self.on_PU_Quit)

        self.PU_Launch = QtGui.QPushButton(self.centralWidget)
        self.PU_Launch.setGeometry(QtCore.QRect(170, 490, 93, 28))
        self.PU_Launch.setToolTip(_fromUtf8("launch the macro and create the spring"))
        self.PU_Launch.setText(_fromUtf8("Launch "))
        self.PU_Launch.setObjectName(_fromUtf8("PU_Launch"))
        self.PU_Launch.clicked.connect(self.on_PU_Launch)

        self.label = QtGui.QLabel(self.centralWidget)
        self.label.setGeometry(QtCore.QRect(23, 0, 280, 31))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setItalic(True)
        font.setUnderline(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setText(_fromUtf8(" Spring Helix Variable "))
        self.label.setObjectName(_fromUtf8("label"))
        MainWindow.setCentralWidget(self.centralWidget)

        self.PU_Reset = QtGui.QPushButton(self.centralWidget)
        self.PU_Reset.setGeometry(QtCore.QRect(100, 490, 61, 28))
        self.PU_Reset.setToolTip(_fromUtf8("Reset the data "))
        self.PU_Reset.setObjectName(_fromUtf8("PU_Reset"))
        self.PU_Reset.clicked.connect(self.on_PU_Reset)

        self.PU_Read = QtGui.QPushButton(self.centralWidget)
        self.PU_Read.setGeometry(QtCore.QRect(10, 460, 121, 28))
        self.PU_Read.setToolTip(_fromUtf8("Read the file "))
        self.PU_Read.setText(_fromUtf8("Read"))
        self.PU_Read.setObjectName(_fromUtf8("on_PU_Read"))
        self.PU_Read.clicked.connect(self.on_PU_Read)

        self.PU_Save = QtGui.QPushButton(self.centralWidget)
        self.PU_Save.setGeometry(QtCore.QRect(140, 460, 121, 28))
        self.PU_Save.setToolTip(_fromUtf8("Save the file"))
        self.PU_Save.setText(_fromUtf8("Save"))
        self.PU_Save.setObjectName(_fromUtf8("on_PU_Save"))
        self.PU_Save.clicked.connect(self.on_PU_Save)

        MainWindow.setCentralWidget(self.centralWidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        try:
            MainWindow.setWindowFlags(PyQt4.QtCore.Qt.WindowStaysOnTopHint)        # PyQt4 cette fonction met la fenêtre en avant
        except Exception:
            MainWindow.setWindowFlags(PySide.QtCore.Qt.WindowStaysOnTopHint)       # PySide cette fonction met la fenêtre en avant

        MainWindow.setWindowTitle("Spring Helix Variable")
        self.groupBox.setTitle("Configure")
        self.label_2.setText("Number of coil")
        self.groupBox_3.setTitle("Type line BSpline")
        self.RA_Wire.setToolTip("Type line Wire")
        self.groupBox_2.setTitle( "Coil special length")
        self.label_7.setText("Pitch of coil")
        self.PU_Accept_Value.setText("Accept the value modified")
        self.textEdit.setText("")
        self.PU_Reset.setText("Reset")

    def on_DS_Numb_Spires(self,value):          # nombre de spire total
        global pas
        global numberSpires
        global helixS
        global radiusS
        numberSpires = value
        helixS = []
        del helixS[:]
        helixS = numberSpires*[pas]
        radiusS = []
        del radiusS[:]
        radiusS = numberSpires*[rayon]
        self.S_Numbering_Spire.setToolTip(_fromUtf8("Numbering of coil for 1 to max = Number of spires ("+str(numberSpires)+")"))
#        App.Console.PrintMessage(str("on_DS_Numb_Spires ")+str(numberSpires)+"\n")

    def on_DS_Radius_Sping(self,value):         # rayon axial du ressort
        global rayon
        rayon = value
#        App.Console.PrintMessage(str("on_DS_Radius_Sping ")+str(rayon)+"\n")

    def on_DS_Pas_Spring(self,value):           # pas (pitch) du ressort
        global pas
        global numberSpires
        global helixS
        global numberSpiresModified
        global pasSpire
        global radiusS
        pas = value
        numberSpiresModified = 0
        self.S_Numbering_Spire.setValue(numberSpiresModified)
        pasSpire = 0.0
        self.DS_Pas_Spire.setValue(pasSpire)
        helixS = []
        del helixS[:]
        helixS = numberSpires*[pas]
        radiusS = []
        del radiusS[:]
        radiusS = numberSpires*[rayon]
#        App.Console.PrintMessage(str("on_DS_Pas_Spring ")+str(pas)+"\n")

    def on_DS_Precision_Turn(self,value):        # " ! float " 360/precision number points for 1 turn
        global precision
        precision = value
        self.label_5.setText(_fromUtf8("Precision = " + str(360/precision) + " points"))
#        App.Console.PrintMessage(str("on_DS_Precision_Turn ")+str(precision)+"\n")

    def on_RA_Wire(self):                        # 
        global typeLine
        typeLine = 1
#        App.Console.PrintMessage(str("on_RA_Wire ")+str(typeLine)+"\n")

    def on_RA_BSpline(self):                     # 
        global typeLine
        typeLine = 0
#        App.Console.PrintMessage(str("on_RA_BSpline ")+str(typeLine)+"\n")

    def on_S_Numbering_Spire(self,value):        # numero de la spire a modifier
        global numberSpires
        global numberSpiresModified
        self.S_Numbering_Spire.setMaximum(numberSpires)
        numberSpiresModified = value
#        App.Console.PrintMessage(str("on_S_Nubering_Spire ")+str(numberSpiresModified)+"\n")

    def on_DS_Pas_Spire(self,value):             # pas (pitch) de la spire a modifier
        global pasSpire
        pasSpire = value
#        App.Console.PrintMessage(str("on_DS_Pas_Spire ")+str(pasSpire)+"\n")

    def on_PU_Accept_Value(self):                # accepter la modification de la spire
        global numberSpiresModified
        global pasSpire
        global rayonSpire
        global helixS
        global radiusS
        if numberSpiresModified == 1:
            helixS[0] = pasSpire
            radiusS[0]= rayonSpire
            self.textEdit.append("Coil number " + str(numberSpiresModified) + " = " + str(helixS[0]) + " " + str(radiusS[0]))
            App.Console.PrintMessage("Coil number " + str(numberSpiresModified) + " = " + str(helixS[0]) + " " + str(radiusS[0])+"\n")
        else:
            helixS[numberSpiresModified-1] = pasSpire
            radiusS[numberSpiresModified-1]= rayonSpire
            self.textEdit.append("Coil number " + str(numberSpiresModified) + " = " + str(helixS[numberSpiresModified-1]) + " " + str(radiusS[numberSpiresModified-1]))
            App.Console.PrintMessage("Coil number " + str(numberSpiresModified) + " = " + str(helixS[numberSpiresModified-1]) + " " + str(radiusS[numberSpiresModified-1])+"\n")
#        App.Console.PrintMessage(str("on_PU_Accept_Value ")+"\n")

    def on_PU_Quit(self):                        # Quit
        App.Console.PrintMessage("\n"+"Fin FCSpring_Helix_Variable"+"\n"+"___________________________"+"\n")
        self.window.hide()
#        App.Console.PrintMessage(str(" ")+str()+"\n")

    def on_PU_Reset(self):                       # Reset
        global numberSpires
        global rayon
        global pas
        global precision
        global typeLine
        global helixS
        global numberSpiresModified
        global pasSpire
        global radiusS
        numberSpires         = 10
        rayon                = 20.0
        pas                  = 15.0
        precision            = 5.0
        typeLine             = 0
        helixS               = 0.0
        numberSpiresModified = 1
        pasSpire             = 0.0

        self.DS_Numb_Spires.setValue(numberSpires)
        self.DS_Radius_Sping.setValue(rayon)
        self.DS_Pas_Spring.setValue(pas)
        self.DS_Precision_Turn.setValue(precision)
        self.RA_BSpline.setChecked(True)
        self.S_Numbering_Spire.setValue(numberSpiresModified)
        self.DS_Pas_Spire.setValue(pasSpire)
        self.textEdit.setText("")
        self.textEdit.clear()
        helixS = []
        del helixS[:]
        helixS = numberSpires*[pas]
        radiusS = []
        del radiusS[:]
        radiusS = numberSpires*[rayon]
#        App.Console.PrintMessage(str("on_PU_Reset ")+"\n")

    def on_PU_Read(self):          # lecture
        global path
        global numberSpires
        global rayon
        global pas
        global precision
        global typeLine
        global helixS
        global radiusS

        OpenName = ""
        try:
            OpenName = QFileDialog.getOpenFileName(None,QString.fromLocal8Bit("Read a file FCSpring"),path,"*.FCSpring") # PyQt4
        except Exception:
            OpenName, Filter = PySide.QtGui.QFileDialog.getOpenFileName(None, "Read a file FCSpring", path, "*.FCSpring")#PySide
        try:
            if OpenName != "":
                try:
                    helixS = []
                    radiusS = []
                    del helixS[:]
                    del radiusS[:]
                    file = open(OpenName, "r") # 
                    Header = file.readline().rstrip('\n\r')
                    if Header == "FCString":
                        self.textEdit.clear()
                        App.Console.PrintMessage(str(Header)+"\n")
                        numberSpires = file.readline().rstrip('\n\r')
                        numberSpires = int(numberSpires)
                        self.DS_Numb_Spires.setValue(numberSpires)
                        App.Console.PrintMessage(str(numberSpires)+"\n")

                        rayon = file.readline().rstrip('\n\r')
                        rayon = float(rayon)
                        radiusS = numberSpires*[rayon]
                        self.DS_Radius_Sping.setValue(rayon)
                        App.Console.PrintMessage(str(rayon)+"\n")

                        pas = file.readline().rstrip('\n\r')
                        pas = float(pas)
                        helixS = numberSpires*[pas]
                        self.DS_Pas_Spring.setValue(pas)
                        App.Console.PrintMessage(str(pas)+"\n")

                        precision = file.readline().rstrip('\n\r')
                        precision = float(precision)
                        self.DS_Precision_Turn.setValue(precision)
                        App.Console.PrintMessage(str(precision)+"\n")

                        typeLine = file.readline().rstrip('\n\r')  # typeLine 0=BSpline or 1=Wire
                        typeLine = int(typeLine)
                        if typeLine == 0:
                            self.RA_BSpline.setChecked(True)
                        else:
                            self.RA_Wire.setChecked(True)

                        dummy = file.readline().rstrip('\n\r')
                        i = 0
                        for ligne in file:
                            a , b = ligne.rstrip('\n\r').split(" ")
                            helixS[i] = float(a)
                            radiusS[i] = float(b)
                            App.Console.PrintMessage(str(i+1)+"  "+str(helixS[i])+"   "+str(radiusS[i])+"\n")
                            self.textEdit.append("Coil number " + str(i+1) + " = " + str(helixS[i]) + " " + str(radiusS[i]))
                            i += 1
                    else:
                        App.Console.PrintMessage("Error file not FCSpring "+"\n")
                        errorDialog("Error file not FCSpring "+OpenName)
                finally:
                    file.close()
        except Exception:
            App.Console.PrintMessage("Error in reading the file "+OpenName+"\n")
            errorDialog("Error in reading the file "+OpenName)

    def on_PU_Save(self):           # enregistrement
        global path
        global numberSpires
        global rayon
        global pas
        global precision
        global typeLine
        global helixS
        global radiusS

        SaveName = ""
        try:
            SaveName = QFileDialog.getSaveFileName(None,QString.fromLocal8Bit("Save a file FCSpring"),path,"*.FCSpring")    # PyQt4
        except Exception:
            SaveName, Filter = PySide.QtGui.QFileDialog.getSaveFileName(None, "Save a file FCSpring", path, "*.FCSpring")   # PySide
        if SaveName == "":
            App.Console.PrintMessage("Process aborted"+"\n")
            errorDialog("Process aborted")
        else:
            App.Console.PrintMessage("Registration of "+SaveName+"\n")
            try:
                file = open(SaveName, 'w') # write
                try:
                    file.write("FCString"+"\n")
                    file.write(str(numberSpires)+"\n")
                    file.write(str(rayon)+"\n")
                    file.write(str(pas)+"\n")
                    file.write(str(precision)+"\n")
                    file.write(str(typeLine)+"\n")
                    file.write("\n")
                    for i in range(numberSpires):
                        file.write(str(helixS[i])+" "+str(radiusS[i])+"\n")
                finally:
                    file.close()
            except Exception:
                App.Console.PrintMessage("Error Registration file "+SaveName+"\n")
                errorDialog("Error Registration file "+SaveName)

    def on_PU_Launch(self):                      # Execute
        global numberSpires
        global rayon
        global pas
        global precision
        global typeLine
        global helixS
        global pasSpire
        global radiusS

        doc = FreeCAD.ActiveDocument
        if doc == None:
            doc = FreeCAD.newDocument()

        vecligne=[FreeCAD.Vector(0,0,0),FreeCAD.Vector(rayon ,0.0,0.0)]          
        ligne = Draft.makeWire(vecligne,closed=False,face=False,support=None)      #creation de la ligne de base
        
        coor_X = coor_Y = coor_Z = 0.0
        angleTr  = 0
        points   = []
        del   points[:]
        pas2 = 0.0
        App.Console.PrintMessage("List " + str(helixS)+"\n")
        App.Console.PrintMessage("___________________________"+"\n")
        
        for spir in range(numberSpires):                                 # number spires Helix
            pas2 = helixS[spir]
            for angleTr in range(0,360,int(precision)):                  #boucle for 1 turn 
                ligne.Placement = FreeCAD.ActiveDocument.Line.Placement=App.Placement(App.Vector(0.0,0.0,0.0), App.Rotation(App.Vector(0,0,1),angleTr), App.Vector(0,0,0))
                a = ligne.Shape.Edges[0].Vertexes[1]                     #fin de ligne
                coor_X = (a.Point.x)
                coor_Y = (a.Point.y)
                points += [FreeCAD.Vector(coor_X,coor_Y,coor_Z)]         #  coordinates makeBSpline
                coor_Z += (pas2 / (360/precision))                       # pas of spring
#                point = Draft.makePoint(coor_X,coor_Y,coor_Z)           #  create point repere for test
        App.ActiveDocument.removeObject(ligne.Name)                      # remove ligne de base directrice

        if typeLine == 1:
            ressort = Draft.makeWire(points,closed=False,face=False,support=None)# creation spring makeWire
        else:
            ressort = Draft.makeBSpline(points,closed=False)                     # creation spring makeBSpline
        FreeCAD.ActiveDocument.recompute()
        
helixS = []
del helixS[:]
helixS = numberSpires*[pas]
radiusS = []
del radiusS[:]
radiusS = numberSpires*[rayon]

MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()

Links

The forum discussion Try to do a Spring

Project

modify diameter to any coil

Version

17/11/2014: ver 1.2 : new version with GUI and modification any coil and save or load the data to disk.

10/11/2014: (23h20) correction of the modification

ligne.Placement = App.Placement(App.Vector(0.0,0.0,0.0), App.Rotation(App.Vector(0,0,1),angleTr), App.Vector(0,0,0))

10/11/2014: modify line 44 :

        a = FreeCAD.ActiveDocument.Line.Placement=App.Placement(App.Vector(0.0,0.0,0.0), App.Rotation(App.Vector(0,0,1),angleTr), App.Vector(0,0,0))

to

        ligne = FreeCAD.ActiveDocument.Line.Placement=App.Placement(App.Vector(0.0,0.0,0.0), App.Rotation(App.Vector(0,0,1),angleTr), App.Vector(0,0,0))

6/11/2014 : adding "makeBSpline" and configuration

Limitations