my second attempt with only one error, getting better

Need help, or want to share a macro? Post here!
sibelius
Posts: 94
Joined: Sat Jan 04, 2020 7:29 am
Location: Leura - NSW _Australia
Contact:

my second attempt with only one error, getting better

Postby sibelius » Thu Feb 13, 2020 11:39 am

Good evening.
This is my second attempt to a dialog window at 10.36 pm; not bad.
Doesn't work but I'm confident; in the main time if someone likes to try, I post the code.
Thanks again to everyone.
Goodnight.
Here is the code.

Code: Select all

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

################################################################################
## Form generated from reading UI file 'widget_2D_functions.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 *
#QUESTA RIGA O BLOCCO SOTTO LA/LO AGGIUNGO DOPO AVER DEFINITO LA FUNZIONE. LA FUNZIONE SI CHIAMA createSolidFunction 
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.view_Step = QLabel(Dialog)
        self.view_Step.setObjectName(u"view_Step")
        self.view_Step.setGeometry(QRect(10, 200, 61, 21))
        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.view_Step_2 = QTextEdit(Dialog)
        self.view_Step_2.setObjectName(u"view_Step_2")
        self.view_Step_2.setGeometry(QRect(80, 190, 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 = QPlainTextEdit(Dialog)
        self.insert_ZT.setObjectName(u"insert_ZT")
        self.insert_ZT.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))
    #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.OK,QtCore.SIGNAL("pressed()"),self.createPlane)
        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_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.view_Step.setText(QCoreApplication.translate("Dialog", u"Step=A/Nb", 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))
    # 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):
        X = float(self.insert_X_2.toPlainText())
        Nb = int (self.insert_Nb_2.toPlainText())
        Step = float (self.view_Step_2.toPlaintext())
        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())
        Z=math.sin(X) + OFFSET
    #definisco i vettori
        points = [Base.Vector(X,Y,0)]
    for I in range(Nb):
        XX=X+I*Step 
        ZZ=math.sin(XX) + 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: 94
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 » Thu Feb 13, 2020 11:42 am

I just saw an error in my code while I was loading related to the button; is not OK but something else.
I'll check again tomorrow.
thanks.
sibelius
Posts: 94
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 » Thu Feb 13, 2020 11:22 pm

Good morning.
I correct some errors in the code but there is something I'm missing.
I get the following error

Code: Select all

  File "<input>", line 136, in createSolidFunction
AttributeError: 'PySide2.QtWidgets.QTextEdit' object has no attribute 'toPlaintext'
.
Is basically the window where i insert my function.
I cant' also display the Step parameter on the window.
As usual any help would be great.
thank you in advance.
here is the code.

Code: Select all

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

################################################################################
## Form generated from reading UI file 'widget_2D_functions.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 *
#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.view_Step = QLabel(Dialog)
        self.view_Step.setObjectName(u"view_Step")
        self.view_Step.setGeometry(QRect(10, 200, 61, 21))
        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.view_Step_2 = QTextEdit(Dialog)
        self.view_Step_2.setObjectName(u"view_Step_2")
        self.view_Step_2.setGeometry(QRect(80, 190, 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 = QPlainTextEdit(Dialog)
        self.insert_ZT.setObjectName(u"insert_ZT")
        self.insert_ZT.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))
    #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_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.view_Step.setText(QCoreApplication.translate("Dialog", u"Step=A/Nb", 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))
    # 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):
        X = float(self.insert_X_2.toPlainText())
        Nb = int (self.insert_Nb_2.toPlainText())
        Step = float (self.view_Step_2.toPlaintext())
        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.toPlainText())
        Z = F(X) + OFFSET
    #definisco i vettori
        points = [Base.Vector(X,Y,0)]
        for I in range(Nb):
            XX=X+I*Step 
            ZZ=F(XX) + 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()   
mario52
Posts: 2804
Joined: Wed May 16, 2012 2:13 pm

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

Postby mario52 » Fri Feb 14, 2020 11:01 am

hi

what to put in "insert function" ?

mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Dialog creation. My macros on Gist.github.
sibelius
Posts: 94
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 » Fri Feb 14, 2020 10:46 pm

Hi Mario
Thanks again for your precious help.
I think I made a bit of a mess of this code.
I'll try to understand the QLineEdit syntax as someone suggested, even if I think with PlainTextEdit should go well either.
Anyway in the insert function the term F(X) should go in.
I have changed this code; for example the face of the function; I putted F instead of F(X) as you can see down below.
So in the PlainTextEdit of the insert function I'd like to put something like (math.sinx(X) or another function like X*X*X or any continuous function); the term Z is basically the function+OFFSET; the OFFSET translate the function of an OFFSET in Z direction.

Code: Select all

 #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

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

A=10
X=-10
Nb=100
Step=A/Nb
Y=0
EXT=5
OFFSET=3
XT=-X
YT=0
ZT=0
RX=0
RY=0
RZ=0
VX=0
VY=0
VZ=0
#Z=math.sin(X) + OFFSET
F=math.sin(X)
Z=F + OFFSET

points = [Base.Vector(X,Y,0)]

for I in range(Nb):
    
    XX=X+I*Step 
    F=math.sin(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)
sibelius
Posts: 94
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 » Fri Feb 14, 2020 11:16 pm

hi
so far this is my last attempt; in the second code you can see the function with math.sin(x) and if you paste it in freeCAD it works.
when I paste the complete code in freeCAD, the windows appear correctly but when I insert the parameter, the python consolle give me an error (I'm sure the Python consolle is right).
I also attached a screenshot of the parameters I set in the window and the function I want to process (X*X).
thanks again.
here is the complete code.

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!
################################################################################

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 = QPlainTextEdit(Dialog)
        self.insert_ZT.setObjectName(u"insert_ZT")
        self.insert_ZT.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))
    #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_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))
    # 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.toPlainTextEdit()) 
        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)]
        for I in range(Nb):
            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

    #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() 

here is the function (exemple done with math.sin(x)

Code: Select all

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

A=10
X=-10
Nb=100
Step=A/Nb
Y=0
EXT=5
OFFSET=3
XT=-X
YT=0
ZT=0
RX=0
RY=0
RZ=0
VX=0
VY=0
VZ=0
#Z=math.sin(X) + OFFSET
F=math.sin(X)
Z=F + OFFSET

points = [Base.Vector(X,Y,0)]

for I in range(Nb):
    
    XX=X+I*Step 
    F=math.sin(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))

Part.show(E)
Attachments
parameters_inserted.png
parameters_inserted.png (13.64 KiB) Viewed 151 times
johnwang
Posts: 65
Joined: Sun Jan 27, 2019 12:41 am

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

Postby johnwang » Sat Feb 15, 2020 2:30 am

Code: Select all

 F = float (self.insert_function_2.toPlainText()) 
This will not work.

"X*X" is a string. You can't convert it into a number. You can keep that string, then think of a way to programly generate that function: F=x*x+offset
sibelius
Posts: 94
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 » Sat Feb 15, 2020 4:43 am

thank you very much for your help.
I will look into that.
thanks again.
PS i'm looking at the syntax of QEditLine but I cant't find at the moment any example to follow.
johnwang
Posts: 65
Joined: Sun Jan 27, 2019 12:41 am

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

Postby johnwang » Sat Feb 15, 2020 4:57 am

sibelius wrote:
Sat Feb 15, 2020 4:43 am
thank you very much for your help.
I will look into that.
thanks again.
PS i'm looking at the syntax of QEditLine but I cant't find at the moment any example to follow.
The tutorial you referenced from uses QLineEdit

Code: Select all

self.height = QtGui.QLineEdit(Dialog)
sibelius
Posts: 94
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 » Sat Feb 15, 2020 7:27 am

Thank you.
Do you know what happened?
I follow two different tutorials and I mixed up a bit.
I need a bit of time to reorganize.
thank you again.
I will post the code in a new post.