my second attempt with only one error, getting better

Need help, or want to share a macro? Post here!
johnwang
Posts: 117
Joined: Sun Jan 27, 2019 12:41 am

Re: my second attempt with only one error, getting better

Postby johnwang » Sat Feb 15, 2020 9:09 am

I think you should keep in the same post.
mario52
Posts: 2857
Joined: Wed May 16, 2012 2:13 pm

Re: my second attempt with only one error, getting better

Postby mario52 » Sat Feb 15, 2020 1:43 pm

hi

try this (o messo uno "#### aggiunto" dove o ....)

Code: Select all

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

################################################################################
## Form generated from reading UI file 'widget_2D_functions2.ui'
##
## Created by: Qt User Interface Compiler version 5.14.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################

####aggiunto
import PySide2
####aggiunto

from PySide2.QtCore import (QCoreApplication, QMetaObject, QObject, QPoint,
    QRect, QSize, QUrl, Qt)
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
    QFontDatabase, QIcon, QLinearGradient, QPalette, QPainter, QPixmap,
    QRadialGradient)
from PySide2.QtWidgets import *
#QUESTA RIGA O BLOCCO SOTTO LA/LO AGGIUNGO DOPO AVER DEFINITO LA FUNZIONE. LA FUNZIONE SI CHIAMA createSolidFunction
#se faccio run da python mi da errore perche' FreeCAD non e' un modulo Python

import FreeCAD, FreeCADGui, Part
import math
from FreeCAD import Base

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        if Dialog.objectName():
            Dialog.setObjectName(u"Dialog")
        Dialog.resize(396, 300)

        self.insert_function = QLabel(Dialog)
        self.insert_function.setObjectName(u"insert_function")
        self.insert_function.setGeometry(QRect(20, 0, 81, 20))
        self.insert_Nb = QLabel(Dialog)
        self.insert_Nb.setObjectName(u"insert_Nb")
        self.insert_Nb.setGeometry(QRect(10, 160, 61, 16))
        self.insert_X = QLabel(Dialog)
        self.insert_X.setObjectName(u"insert_X")
        self.insert_X.setGeometry(QRect(20, 80, 47, 13))
        self.insert_EXT = QLabel(Dialog)
        self.insert_EXT.setObjectName(u"insert_EXT")
        self.insert_EXT.setGeometry(QRect(280, 140, 47, 13))
        self.insert_OFFSET = QLabel(Dialog)
        self.insert_OFFSET.setObjectName(u"insert_OFFSET")
        self.insert_OFFSET.setGeometry(QRect(10, 240, 81, 16))
        self.insert_A = QLabel(Dialog)
        self.insert_A.setObjectName(u"insert_A")
        self.insert_A.setGeometry(QRect(20, 120, 47, 13))
        self.insert_XT = QLabel(Dialog)
        self.insert_XT.setObjectName(u"insert_XT")
        self.insert_XT.setGeometry(QRect(170, 20, 47, 13))
        self.insert_YT = QLabel(Dialog)
        self.insert_YT.setObjectName(u"insert_YT")
        self.insert_YT.setGeometry(QRect(170, 60, 47, 13))
        self.insert_ZX = QLabel(Dialog)
        self.insert_ZX.setObjectName(u"insert_ZX")
        self.insert_ZX.setGeometry(QRect(170, 100, 47, 13))
        self.insert_RX = QLabel(Dialog)
        self.insert_RX.setObjectName(u"insert_RX")
        self.insert_RX.setGeometry(QRect(170, 140, 47, 13))
        self.insert_RY = QLabel(Dialog)
        self.insert_RY.setObjectName(u"insert_RY")
        self.insert_RY.setGeometry(QRect(170, 180, 47, 13))
        self.insert_RZ = QLabel(Dialog)
        self.insert_RZ.setObjectName(u"insert_RZ")
        self.insert_RZ.setGeometry(QRect(170, 220, 47, 13))
        self.insert_VX = QLabel(Dialog)
        self.insert_VX.setObjectName(u"insert_VX")
        self.insert_VX.setGeometry(QRect(290, 20, 47, 13))
        self.insert_VY = QLabel(Dialog)
        self.insert_VY.setObjectName(u"insert_VY")
        self.insert_VY.setGeometry(QRect(290, 60, 47, 13))
        self.insert_VZ = QLabel(Dialog)
        self.insert_VZ.setObjectName(u"insert_VZ")
        self.insert_VZ.setGeometry(QRect(290, 100, 47, 13))

        self.insert_function_2 = QPlainTextEdit(Dialog)
        self.insert_function_2.setObjectName(u"insert_function_2")
        self.insert_function_2.setGeometry(QRect(20, 20, 104, 41))
        self.insert_X_2 = QPlainTextEdit(Dialog)
        self.insert_X_2.setObjectName(u"insert_X_2")
        self.insert_X_2.setGeometry(QRect(80, 70, 41, 31))
        self.insert_A_2 = QPlainTextEdit(Dialog)
        self.insert_A_2.setObjectName(u"insert_A_2")
        self.insert_A_2.setGeometry(QRect(80, 110, 41, 31))
        self.insert_Nb_2 = QPlainTextEdit(Dialog)
        self.insert_Nb_2.setObjectName(u"insert_Nb_2")
        self.insert_Nb_2.setGeometry(QRect(80, 150, 41, 31))
        self.insert_OFFSET_2 = QPlainTextEdit(Dialog)
        self.insert_OFFSET_2.setObjectName(u"insert_OFFSET_2")
        self.insert_OFFSET_2.setGeometry(QRect(80, 230, 41, 31))
        self.insert_EXT_2 = QPlainTextEdit(Dialog)
        self.insert_EXT_2.setObjectName(u"insert_EXT_2")
        self.insert_EXT_2.setGeometry(QRect(340, 130, 41, 31))
        self.insert_XT_2 = QTextEdit(Dialog)
        self.insert_XT_2.setObjectName(u"insert_XT_2")
        self.insert_XT_2.setGeometry(QRect(220, 10, 41, 31))
        self.insert_VX_2 = QPlainTextEdit(Dialog)
        self.insert_VX_2.setObjectName(u"insert_VX_2")
        self.insert_VX_2.setGeometry(QRect(340, 10, 41, 31))
        self.insert_YT_2 = QPlainTextEdit(Dialog)
        self.insert_YT_2.setObjectName(u"insert_YT_2")
        self.insert_YT_2.setGeometry(QRect(220, 50, 41, 31))
        
        self.insert_ZT_2 = QPlainTextEdit(Dialog)
        self.insert_ZT_2.setObjectName(u"insert_ZT")
        self.insert_ZT_2.setGeometry(QRect(220, 90, 41, 31))
        
        self.insert_RX_2 = QPlainTextEdit(Dialog)
        self.insert_RX_2.setObjectName(u"insert_RX_2")
        self.insert_RX_2.setGeometry(QRect(220, 130, 41, 31))
        self.insert_RY_2 = QPlainTextEdit(Dialog)
        self.insert_RY_2.setObjectName(u"insert_RY_2")
        self.insert_RY_2.setGeometry(QRect(220, 170, 41, 31))
        self.insert_RZ_2 = QPlainTextEdit(Dialog)
        self.insert_RZ_2.setObjectName(u"insert_RZ_2")
        self.insert_RZ_2.setGeometry(QRect(220, 210, 41, 31))
        self.insert_VY_2 = QPlainTextEdit(Dialog)
        self.insert_VY_2.setObjectName(u"insert_VY_2")
        self.insert_VY_2.setGeometry(QRect(340, 50, 41, 31))
        self.insert_VZ_2 = QPlainTextEdit(Dialog)
        self.insert_VZ_2.setObjectName(u"insert_VZ_2")
        self.insert_VZ_2.setGeometry(QRect(340, 90, 41, 31))
        self.invio_OK = QPushButton(Dialog)
        self.invio_OK.setObjectName(u"invio_OK")
        self.invio_OK.setGeometry(QRect(290, 240, 75, 23))
    #inserisco questa riga tra le due righe per dire a FreeCAd di collegare il bottone alla funzione
        QtCore.QObject.connect(self.invio_OK,QtCore.SIGNAL("pressed()"),self.createSolidFunction)

####agggiunto
        self.invio_Quit = QPushButton(Dialog)
        self.invio_Quit.setObjectName(u"invio_Quit")
        self.invio_Quit.setGeometry(QRect(290, 265, 75, 23))
        self.invio_Quit.clicked.connect(self.Ciao) ###
####agggiunto

        QMetaObject.connectSlotsByName(Dialog)
    #UI Dialog
        self.retranslateUi(Dialog)
    # setupUi
    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QCoreApplication.translate("Dialog", u"Dialog", None))
####aggiunto
        Dialog.setWindowFlags(PySide2.QtCore.Qt.WindowStaysOnTopHint)         # PySide2 cette fonction met la fenetre en avant
####aggiunto
        Dialog.setWindowTitle("Dialog ")

#        self.insert_function.setText(QCoreApplication.translate("Dialog", u"insert function", None))
#        self.insert_Nb.setText(QCoreApplication.translate("Dialog", u"insert Nb", None))
#        self.insert_X.setText(QCoreApplication.translate("Dialog", u"insert X", None))
#        self.insert_EXT.setText(QCoreApplication.translate("Dialog", u"insert EXT", None))
#        self.insert_OFFSET.setText(QCoreApplication.translate("Dialog", u"insert OFFSET", None))
#        self.insert_A.setText(QCoreApplication.translate("Dialog", u"insert A", None))
#        self.insert_XT.setText(QCoreApplication.translate("Dialog", u"insert XT", None))
#        self.insert_YT.setText(QCoreApplication.translate("Dialog", u"insert YT", None))
#        self.insert_ZX.setText(QCoreApplication.translate("Dialog", u"insert ZT", None))
#        self.insert_RX.setText(QCoreApplication.translate("Dialog", u"insert RX", None))
#        self.insert_RY.setText(QCoreApplication.translate("Dialog", u"insert RY", None))
#        self.insert_RZ.setText(QCoreApplication.translate("Dialog", u"insert RZ", None))
#        self.insert_VX.setText(QCoreApplication.translate("Dialog", u"insert VX", None))
#        self.insert_VY.setText(QCoreApplication.translate("Dialog", u"insert VY", None))
#        self.insert_VZ.setText(QCoreApplication.translate("Dialog", u"insert VZ", None))
#        self.invio_OK.setText(QCoreApplication.translate("Dialog", u"OK", None))

####cambiato con
        _translate = QtCore.QCoreApplication.translate

        self.insert_function.setText(_translate("Dialog", u"insert function"))
        self.insert_Nb.setText(_translate("Dialog", u"insert Nb"))
        self.insert_X.setText(_translate("Dialog", u"insert X"))
        self.insert_EXT.setText(_translate("Dialog", u"insert EXT"))
        self.insert_OFFSET.setText(_translate("Dialog", u"insert OFFSET"))
        self.insert_A.setText(_translate("Dialog", u"insert A"))
        self.insert_XT.setText(_translate("Dialog", u"insert XT"))
        self.insert_YT.setText(_translate("Dialog", u"insert YT"))
        self.insert_ZX.setText(_translate("Dialog", u"insert ZT"))
        self.insert_RX.setText(_translate("Dialog", u"insert RX"))
        self.insert_RY.setText(_translate("Dialog", u"insert RY"))
        self.insert_RZ.setText(_translate("Dialog", u"insert RZ"))
        self.insert_VX.setText(_translate("Dialog", u"insert VX"))
        self.insert_VY.setText(_translate("Dialog", u"insert VY"))
        self.insert_VZ.setText(_translate("Dialog", u"insert VZ"))
        self.invio_OK.setText(_translate("Dialog", u"OK"))
        self.invio_Quit.setText(_translate("Dialog", u"Ciao"))
####cambiato con


    # retranslateUi
    #QUESTA SOTTO E' LA FUNZIONE PER FARGLI FARE QUALCOSA
    #XT,YT,ZT sono parametri di traslazione; RX,RY,RZ sono parametri di rotazione
    #attorno agli assi; VX,VY,VZ sono parametri che indicano un vettore rispetto al
    #quale si posizione il solido; i parametri X indicano il punto iniziale del
    #dominio di f; Nb e' un parametro da cui dipende il numero degli step; EXT mi
    #da la dimensione da estrudere
    def createSolidFunction(self):

######## se errore dovete fare uno test "if == 0 .....        

        A = float(self.insert_A_2.toPlainText()) 
        X = float(self.insert_X_2.toPlainText())
        Nb = float (self.insert_Nb_2.toPlainText())
        Step = A/Nb
        Y = 0
        
        EXT = float(self.insert_EXT_2.toPlainText())
        OFFSET = float (self.insert_OFFSET_2.toPlainText())
        XT = float (self.insert_XT_2.toPlainText())
        YT = float (self.insert_YT_2.toPlainText())
        ZT = float (self.insert_ZT_2.toPlainText())
        RX = float (self.insert_RX_2.toPlainText())
        RY = float (self.insert_RY_2.toPlainText())
        RZ = float (self.insert_RZ_2.toPlainText())
        VX = float (self.insert_VX_2.toPlainText())
        VY = float (self.insert_VY_2.toPlainText())
        VZ = float (self.insert_VZ_2.toPlainText())
        F = float (self.insert_function_2.toPlainText())
        Z = F + OFFSET
    #definisco i vettori
        points = [Base.Vector(X,Y,0)]

###### agiuto
#        for I in range(Nb):
#        for I in range(int(Nb)): #non puo essere un float() ma uno int(); o fatte il seguente:
        for I in range(int(Nb*1000)): # per 3 decimale
            I = float(I/1000.0)
            print(I)
######
            XX=X+I*Step 
            ZZ=F + OFFSET
            points.append(Base.Vector(XX,Y,ZZ))
        points.append(Base.Vector(XX,Y,0))
        points.append(points[0])
        poly = Part.makePolygon(points)
        face = Part.Face(poly)
        E = face.extrude(Base.Vector(0,EXT,0))
    #E.Placement=App.Placement(App.Vector(XT,YT,ZT), App.Rotation(RX,RY,RZ), App.Vector(VX,VY,VZ))
    #Gui.activeDocument().activeView().viewIsometric()
    #Gui.ActiveDocument.ActiveView.setAxisCross(True)
        Part.show(E)
    #QUI FINISCE LA FUNZIONE

####aggiunto
    def Ciao(self):
        print("Fine, Ciao")
        MainWindow.hide() 
####aggiunto
    #aggiunta di Mario per visualizzare la finestra in freeCAD

from PySide2 import QtWidgets, QtCore 
if __name__ == "__main__":
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_Dialog()
    ui.setupUi(MainWindow)
    MainWindow.show()

mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Dialog creation. My macros on Gist.github.
sibelius
Posts: 100
Joined: Sat Jan 04, 2020 7:29 am
Location: Leura - NSW _Australia
Contact:

Re: my second attempt with only one error, getting better

Postby sibelius » Sun Feb 16, 2020 2:48 am

Thank you very much Mario for your precious help.
Your code give to me about the same error as mine, but I have to say I really like the button in the end; I'm learning something everyday.
I think the problem is in a variable called F in my createSolidFunction.
I think when I type in the window's box for example the function X*X, this is a string and I cant' get a float in a way I define F.
Basically when I put X*X I would expect a float return.
So in that box I'd like to put the string expression of my basic function called F and at the same time get float operations.
I'm working at the moment at a small code to try to solve this problem in a small scale.
I think I have to spend more time to try to understand Mario's code; unfortunately my skills are not up to at the moment.
The two files down below are my working in progress test for this problem (they don't run on freeCAD; you could tested on a terminal python consolle.)
Anyone who like to test this files; have to copy each code into at two different py file; one named piano3.py and the other called piano3Main.py; After done that you can run the file piano3Main.py and get the window to play.
I included different functions in the Main file; to test each of them you can change the name of the function linked to the button.
I also provide the ui file called piano3.ui witch is called by the Main function.
Note that the three files have to be in the same folder.
Thank you very much for your help.
Much appreciate.
File called piano3.py (it doesn't make a piano--just a name)

Code: Select all

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

################################################################################
## Form generated from reading UI file 'piano2.ui'
##
## Created by: Qt User Interface Compiler version 5.14.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################

from PySide2.QtCore import (QCoreApplication, QMetaObject, QObject, QPoint,
    QRect, QSize, QUrl, Qt)
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
    QFontDatabase, QIcon, QLinearGradient, QPalette, QPainter, QPixmap,
    QRadialGradient)
from PySide2.QtWidgets import *

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        if Dialog.objectName():
            Dialog.setObjectName(u"Dialog")
        Dialog.resize(400, 300)
        self.larghezza = QLabel(Dialog)
        self.larghezza.setObjectName(u"larghezza")
        self.larghezza.setGeometry(QRect(40, 120, 47, 13))
        self.altezza = QLabel(Dialog)
        self.altezza.setObjectName(u"altezza")
        self.altezza.setGeometry(QRect(40, 50, 47, 13))
        self.piano = QLabel(Dialog)
        self.piano.setObjectName(u"piano")
        self.piano.setGeometry(QRect(40, 10, 47, 13))
        self.altezza_inserisci = QPlainTextEdit(Dialog)
        self.altezza_inserisci.setObjectName(u"altezza_inserisci")
        self.altezza_inserisci.setGeometry(QRect(100, 40, 61, 31))
        self.larghezza_inserisci = QPlainTextEdit(Dialog)
        self.larghezza_inserisci.setObjectName(u"larghezza_inserisci")
        self.larghezza_inserisci.setGeometry(QRect(100, 110, 61, 31))
        self.OK = QPushButton(Dialog)
        self.OK.setObjectName(u"OK")
        self.OK.setGeometry(QRect(70, 190, 75, 23))
        self.FUNZIONE_2 = QPlainTextEdit(Dialog)
        self.FUNZIONE_2.setObjectName(u"FUNZIONE_2")
        self.FUNZIONE_2.setGeometry(QRect(220, 50, 104, 71))
        self.FUNZIONE = QLabel(Dialog)
        self.FUNZIONE.setObjectName(u"FUNZIONE")
        self.FUNZIONE.setGeometry(QRect(230, 20, 71, 16))
        self.RISULTATO_2 = QTextEdit(Dialog)
        self.RISULTATO_2.setObjectName(u"RISULTATO_2")
        self.RISULTATO_2.setGeometry(QRect(220, 160, 104, 71))
        self.RISULTATO = QLabel(Dialog)
        self.RISULTATO.setObjectName(u"RISULTATO")
        self.RISULTATO.setGeometry(QRect(220, 140, 71, 16))

        self.retranslateUi(Dialog)

        QMetaObject.connectSlotsByName(Dialog)
    # setupUi

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QCoreApplication.translate("Dialog", u"Dialog", None))
        self.larghezza.setText(QCoreApplication.translate("Dialog", u"larghezza", None))
        self.altezza.setText(QCoreApplication.translate("Dialog", u"altezza", None))
        self.piano.setText(QCoreApplication.translate("Dialog", u"piano", None))
        self.OK.setText(QCoreApplication.translate("Dialog", u"OK", None))
        self.FUNZIONE.setText(QCoreApplication.translate("Dialog", u"FUNZIONE", None))
        self.RISULTATO.setText(QCoreApplication.translate("Dialog", u"RISULTATO", None))
    # retranslateUi

File called piano3Main.py

Code: Select all

###Be carefully you can test this functions only in the box called FUNZIONE
import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.uic import loadUi

class MainPage(QDialog):
    def __init__(self):
        super(MainPage,self).__init__()
        loadUi('piano3.ui', self)
    #collego il bottone alla funzione
    #   self.OK.clicked.connect(self.la funzione che voglio collegare)
        self.OK.clicked.connect(self.collect_strings)
        
    #QUESTA SOTTO SONO LE FUNZIONI


    def collect_float(self):
           numeri = self.FUNZIONE_2.toPlainText()
           numeri = float(numeri)*2
           self.RISULTATO_2.setText(str(numeri))
    #QUI FINISCE LA FUNZIONE



    def collect_strings(self):
           parole = self.FUNZIONE_2.toPlainText()
           self.RISULTATO_2.setText(parole)  
     #QUI FINISCE LA FUNZIONE


"""
    def string_to_number(self):
           X = 10
           Y = 20
   
           parole = self.FUNZIONE_2.toPlainText()
           self.RISULTATO_2.setText(parole)  
     #QUI FINISCE LA FUNZIONE
"""

app = QApplication(sys.argv)
widget = MainPage()
widget.show()
sys.exit(app.exec_()) 
Attachments
piano3.ui
(2.93 KiB) Downloaded 1 time
johnwang
Posts: 117
Joined: Sun Jan 27, 2019 12:41 am

Re: my second attempt with only one error, getting better

Postby johnwang » Sun Feb 16, 2020 3:08 am

This one, I removed the function part.

Code: Select all

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

################################################################################
## Form generated from reading UI file 'widget_2D_functions2.ui'
##
## Created by: Qt User Interface Compiler version 5.14.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################

#https://forum.freecadweb.org/viewtopic.php?f=22&t=43341

from PySide2.QtCore import (QCoreApplication, QMetaObject, QObject, QPoint,
    QRect, QSize, QUrl, Qt)
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
    QFontDatabase, QIcon, QLinearGradient, QPalette, QPainter, QPixmap,
    QRadialGradient)
from PySide2.QtWidgets import *
#QUESTA RIGA O BLOCCO SOTTO LA/LO AGGIUNGO DOPO AVER DEFINITO LA FUNZIONE. LA FUNZIONE SI CHIAMA createSolidFunction
#se faccio run da python mi da errore perche' FreeCAD non e' un modulo Python

import FreeCAD, FreeCADGui, Part
import math
from FreeCAD import Base


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        if Dialog.objectName():
            Dialog.setObjectName(u"Dialog")
        Dialog.resize(396, 300)

        #self.insert_function = QLabel(Dialog)
        #self.insert_function.setObjectName(u"insert_function")
        #self.insert_function.setGeometry(QRect(20, 0, 81, 20))

        self.insert_X = QLabel(Dialog)
        self.insert_X.setObjectName(u"insert_X")
        self.insert_X.setGeometry(QRect(20, 80, 47, 13))

        self.insert_A = QLabel(Dialog)
        self.insert_A.setObjectName(u"insert_A")
        self.insert_A.setGeometry(QRect(20, 120, 47, 13))

        self.insert_Nb = QLabel(Dialog)
        self.insert_Nb.setObjectName(u"insert_Nb")
        self.insert_Nb.setGeometry(QRect(10, 160, 61, 16))

        self.insert_OFFSET = QLabel(Dialog)
        self.insert_OFFSET.setObjectName(u"insert_OFFSET")
        self.insert_OFFSET.setGeometry(QRect(10, 240, 81, 16))

        self.insert_XT = QLabel(Dialog)
        self.insert_XT.setObjectName(u"insert_XT")
        self.insert_XT.setGeometry(QRect(170, 20, 47, 13))
        self.insert_YT = QLabel(Dialog)
        self.insert_YT.setObjectName(u"insert_YT")
        self.insert_YT.setGeometry(QRect(170, 60, 47, 13))
        self.insert_ZT = QLabel(Dialog)
        self.insert_ZT.setObjectName(u"insert_ZT")
        self.insert_ZT.setGeometry(QRect(170, 100, 47, 13))
        self.insert_RX = QLabel(Dialog)
        self.insert_RX.setObjectName(u"insert_RX")
        self.insert_RX.setGeometry(QRect(170, 140, 47, 13))
        self.insert_RY = QLabel(Dialog)
        self.insert_RY.setObjectName(u"insert_RY")
        self.insert_RY.setGeometry(QRect(170, 180, 47, 13))
        self.insert_RZ = QLabel(Dialog)
        self.insert_RZ.setObjectName(u"insert_RZ")
        self.insert_RZ.setGeometry(QRect(170, 220, 47, 13))

        self.insert_VX = QLabel(Dialog)
        self.insert_VX.setObjectName(u"insert_VX")
        self.insert_VX.setGeometry(QRect(290, 20, 47, 13))
        self.insert_VY = QLabel(Dialog)
        self.insert_VY.setObjectName(u"insert_VY")
        self.insert_VY.setGeometry(QRect(290, 60, 47, 13))
        self.insert_VZ = QLabel(Dialog)
        self.insert_VZ.setObjectName(u"insert_VZ")
        self.insert_VZ.setGeometry(QRect(290, 100, 47, 13))

        self.insert_EXT = QLabel(Dialog)
        self.insert_EXT.setObjectName(u"insert_EXT")
        self.insert_EXT.setGeometry(QRect(280, 140, 47, 13))


        #self.insert_function_2 = QLineEdit(Dialog)
        #self.insert_function_2.setObjectName(u"insert_function_2")
        #self.insert_function_2.setGeometry(QRect(20, 20, 104, 41))

        self.insert_X_2 = QLineEdit(Dialog)
        self.insert_X_2.setObjectName(u"insert_X_2")
        self.insert_X_2.setGeometry(QRect(80, 70, 41, 31))
        self.insert_X_2.setText("-10")

        self.insert_A_2 = QLineEdit(Dialog)
        self.insert_A_2.setObjectName(u"insert_A_2")
        self.insert_A_2.setGeometry(QRect(80, 110, 41, 31))
        self.insert_A_2.setText("10")

        self.insert_Nb_2 = QLineEdit(Dialog)
        self.insert_Nb_2.setObjectName(u"insert_Nb_2")
        self.insert_Nb_2.setGeometry(QRect(80, 150, 41, 31))
        self.insert_Nb_2.setText("100")

        self.insert_OFFSET_2 = QLineEdit(Dialog)
        self.insert_OFFSET_2.setObjectName(u"insert_OFFSET_2")
        self.insert_OFFSET_2.setGeometry(QRect(80, 230, 41, 31))
        self.insert_OFFSET_2.setText("5")


        self.insert_XT_2 = QLineEdit(Dialog)
        self.insert_XT_2.setObjectName(u"insert_XT_2")
        self.insert_XT_2.setGeometry(QRect(220, 10, 41, 31))
        self.insert_XT_2.setText("10")
        self.insert_YT_2 = QLineEdit(Dialog)
        self.insert_YT_2.setObjectName(u"insert_YT_2")
        self.insert_YT_2.setGeometry(QRect(220, 50, 41, 31))
        self.insert_YT_2.setText("0")
        self.insert_ZT_2 = QLineEdit(Dialog)
        self.insert_ZT_2.setObjectName(u"insert_ZT_2")
        self.insert_ZT_2.setGeometry(QRect(220, 90, 41, 31))
        self.insert_ZT_2.setText("0")
        self.insert_RX_2 = QLineEdit(Dialog)
        self.insert_RX_2.setObjectName(u"insert_RX_2")
        self.insert_RX_2.setGeometry(QRect(220, 130, 41, 31))
        self.insert_RX_2.setText("0")
        self.insert_RY_2 = QLineEdit(Dialog)
        self.insert_RY_2.setObjectName(u"insert_RY_2")
        self.insert_RY_2.setGeometry(QRect(220, 170, 41, 31))
        self.insert_RY_2.setText("0")
        self.insert_RZ_2 = QLineEdit(Dialog)
        self.insert_RZ_2.setObjectName(u"insert_RZ_2")
        self.insert_RZ_2.setGeometry(QRect(220, 210, 41, 31))
        self.insert_RZ_2.setText("0")


        self.insert_VX_2 = QLineEdit(Dialog)
        self.insert_VX_2.setObjectName(u"insert_VX_2")
        self.insert_VX_2.setGeometry(QRect(340, 10, 41, 31))
        self.insert_VX_2.setText("0")
        self.insert_VY_2 = QLineEdit(Dialog)
        self.insert_VY_2.setObjectName(u"insert_VY_2")
        self.insert_VY_2.setGeometry(QRect(340, 50, 41, 31))
        self.insert_VY_2.setText("0")
        self.insert_VZ_2 = QLineEdit(Dialog)
        self.insert_VZ_2.setObjectName(u"insert_VZ_2")
        self.insert_VZ_2.setGeometry(QRect(340, 90, 41, 31))
        self.insert_VZ_2.setText("0")

        self.insert_EXT_2 = QLineEdit(Dialog)
        self.insert_EXT_2.setObjectName(u"insert_EXT_2")
        self.insert_EXT_2.setGeometry(QRect(340, 130, 41, 31))
        self.insert_EXT_2.setText("5")


        self.invio_OK = QPushButton(Dialog)
        self.invio_OK.setObjectName(u"invio_OK")
        self.invio_OK.setGeometry(QRect(290, 240, 75, 23))
    #UI Dialog
        self.retranslateUi(Dialog)
    #inserisco questa riga tra le due righe per dire a FreeCAd di collegare il bottone alla funzione
        QtCore.QObject.connect(self.invio_OK,QtCore.SIGNAL("pressed()"),self.createSolidFunction)
        QMetaObject.connectSlotsByName(Dialog)
    # setupUi
    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QCoreApplication.translate("Dialog", u"Dialog", None))

        #self.insert_function.setText(QCoreApplication.translate("Dialog", u"insert function", None))

        self.insert_X.setText(QCoreApplication.translate("Dialog", u"insert X", None))
        self.insert_A.setText(QCoreApplication.translate("Dialog", u"insert A", None))
        self.insert_Nb.setText(QCoreApplication.translate("Dialog", u"insert Nb", None))
        self.insert_OFFSET.setText(QCoreApplication.translate("Dialog", u"insert OFFSET", None))

        self.insert_XT.setText(QCoreApplication.translate("Dialog", u"insert XT", None))
        self.insert_YT.setText(QCoreApplication.translate("Dialog", u"insert YT", None))
        self.insert_ZT.setText(QCoreApplication.translate("Dialog", u"insert ZT", None))
        self.insert_RX.setText(QCoreApplication.translate("Dialog", u"insert RX", None))
        self.insert_RY.setText(QCoreApplication.translate("Dialog", u"insert RY", None))
        self.insert_RZ.setText(QCoreApplication.translate("Dialog", u"insert RZ", None))

        self.insert_VX.setText(QCoreApplication.translate("Dialog", u"insert VX", None))
        self.insert_VY.setText(QCoreApplication.translate("Dialog", u"insert VY", None))
        self.insert_VZ.setText(QCoreApplication.translate("Dialog", u"insert VZ", None))
        self.insert_EXT.setText(QCoreApplication.translate("Dialog", u"insert EXT", None))

        self.invio_OK.setText(QCoreApplication.translate("Dialog", u"OK", None))
    # retranslateUi
    #QUESTA SOTTO E' LA FUNZIONE PER FARGLI FARE QUALCOSA
    #XT,YT,ZT sono parametri di traslazione; RX,RY,RZ sono parametri di rotazione
    #attorno agli assi; VX,VY,VZ sono parametri che indicano un vettore rispetto al
    #quale si posizione il solido; i parametri X indicano il punto iniziale del
    #dominio di f; Nb e' un parametro da cui dipende il numero degli step; EXT mi
    #da la dimensione da estrudere
    def createSolidFunction(self):
        A = float(self.insert_A_2.text()) 
        X = float(self.insert_X_2.text())
        Nb = int (self.insert_Nb_2.text())
        Step = A/Nb
        Y = 0
        EXT = float (self.insert_EXT_2.text())
        OFFSET = float (self.insert_OFFSET_2.text())
        XT = float (self.insert_XT_2.text())
        YT = float (self.insert_YT_2.text())
        ZT = float (self.insert_ZT_2.text())
        RX = float (self.insert_RX_2.text())
        RY = float (self.insert_RY_2.text())
        RZ = float (self.insert_RZ_2.text())
        VX = float (self.insert_VX_2.text())
        VY = float (self.insert_VY_2.text())
        VZ = float (self.insert_VZ_2.text())

        #F =  float(self.insert_function_2.text())
        #Z =X*X + OFFSET
    #definisco i vettori
        points = [Base.Vector(X,Y,0)]
        for I in range(Nb):
            XX=X+I*Step 
            F=XX*XX
            ZZ=F + OFFSET
            points.append(Base.Vector(XX,Y,ZZ))
        points.append(Base.Vector(XX,Y,0))
        points.append(points[0])
        poly = Part.makePolygon(points)
        face = Part.Face(poly)
        E = face.extrude(Base.Vector(0,EXT,0))
    #E.Placement=App.Placement(App.Vector(XT,YT,ZT), App.Rotation(RX,RY,RZ), App.Vector(VX,VY,VZ))
    #Gui.activeDocument().activeView().viewIsometric()
    #Gui.ActiveDocument.ActiveView.setAxisCross(True)
        Part.show(E)
    #QUI FINISCE LA FUNZIONE

    #aggiunta di Mario per visualizzare la finestra in freeCAD

from PySide2 import QtWidgets, QtCore 
if __name__ == "__main__":
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_Dialog()
    ui.setupUi(MainWindow)
    MainWindow.show()
    #MainWindow.hide() 
sibelius
Posts: 100
Joined: Sat Jan 04, 2020 7:29 am
Location: Leura - NSW _Australia
Contact:

Re: my second attempt with only one error, getting better

Postby sibelius » Sun Feb 16, 2020 10:55 am

Thank you very much for your help John.
Your code is working very well.
Now my next goal is to create a Box in the window where I can put any "regular functions"; with "regular function" I mean any continuous function in his field of existence.
X*X is a good example but if I want to change the face of this function I have to interviene inside the code.
It would be nice to manage to change the function from a box directly in a window-box.
Anyway, thank you very much again for your help.
As soon as possible I'm going to study yours code too.
sibelius
Posts: 100
Joined: Sat Jan 04, 2020 7:29 am
Location: Leura - NSW _Australia
Contact:

Re: my second attempt with only one error, getting better

Postby sibelius » Mon Feb 17, 2020 11:29 pm

Hi everybody.
I think to address this problem I might need a SymPy Python library.
In my understanding is an easy way to avoid creating a parser.
I'm just studying this library.
I will be back when I'll upgrade myself.
Ciao.
Last edited by sibelius on Thu Feb 20, 2020 5:15 am, edited 1 time in total.
mario52
Posts: 2857
Joined: Wed May 16, 2012 2:13 pm

Re: my second attempt with only one error, getting better

Postby mario52 » Tue Feb 18, 2020 9:33 am

hi

see this easy GUI macro for example Macro_3D_Parametric_Curve

other Mathematical_functions

mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Dialog creation. My macros on Gist.github.
sibelius
Posts: 100
Joined: Sat Jan 04, 2020 7:29 am
Location: Leura - NSW _Australia
Contact:

Re: my second attempt with only one error, getting better

Postby sibelius » Tue Feb 18, 2020 10:45 am

Hi Mario Thank you very much for your help.
I will have a look and study the macro.
Thanks again.
ciao
sibelius
Posts: 100
Joined: Sat Jan 04, 2020 7:29 am
Location: Leura - NSW _Australia
Contact:

Re: my second attempt with only one error, getting better

Postby sibelius » Wed Feb 19, 2020 10:05 pm

Good morning to everyone.
I'm going to post the new code piano3Main.py (the code I use to try to solve the problem in the other code).
This code working and I solve the problem importing the module sympy.
Next step is to adjust the other code in the same way.
Here is the code

Code: Select all

###Be carefully you can test this functions only in the box called FUNZIONE
import sys
#riga (import sympy) aggiunta da me
import sympy
#riga (from sympy import *) aggiunta da me
from sympy import *
#
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.uic import loadUi

class MainPage(QDialog):
    def __init__(self):
        super(MainPage,self).__init__()
        loadUi('piano3.ui', self)
    #collego il bottone alla funzione
    #   self.OK.clicked.connect(self.la funzione che voglio collegare)
        self.OK.clicked.connect(self.string_to_number)
        
    #QUESTA SOTTO SONO LE FUNZIONI

    #questa funzione ritorna un numero dall'inserimento di un numero dopo un'operazione matematica
    def collect_float(self):
           numeri = self.FUNZIONE_2.toPlainText()
           numeri = float(numeri)*2
           self.RISULTATO_2.setText(str(numeri))
    #QUI FINISCE LA FUNZIONE


    #questa funzione ritorna una stringa dall'inserimento di una stringa
    def collect_strings(self):
           parole = self.FUNZIONE_2.toPlainText()
           self.RISULTATO_2.setText(parole)  
     #QUI FINISCE LA FUNZIONE



    def string_to_number(self):
           x = symbols('x') 
           numeri = sympify(self.FUNZIONE_2.toPlainText()).subs(x,3).evalf(4) 
     #     self.RISULTATO_2.setText(str(print(numeri)))
           self.RISULTATO_2.setText(str(numeri))
     #QUI FINISCE LA FUNZIONE


app = QApplication(sys.argv)
widget = MainPage()
widget.show()
sys.exit(app.exec_())


"""
### CODICE DI PROVA OK
#riga (import sympy) aggiunta da me
import sympy
#riga (from sympy import *) aggiunta da me
from sympy import *

x = symbols('x') 
espressione = sympify('x*2.71')
w = espressione.subs(x,3).evalf(4)
pprint(espressione)
"""

"""
### CODICE DI PROVA2 OK
#riga (import sympy) aggiunta da me
import sympy
#riga (from sympy import *) aggiunta da me
from sympy import *

x = symbols('x') 
e = sympify('x*3')
pprint(e)
"""




johnwang
Posts: 117
Joined: Sun Jan 27, 2019 12:41 am

Re: my second attempt with only one error, getting better

Postby johnwang » Thu Feb 20, 2020 3:16 am

How to install sympy for FreeCAD?