Line Drawing Function not working

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
Post Reply
SBlade
Posts: 40
Joined: Fri May 01, 2020 8:38 pm

Line Drawing Function not working

Post by SBlade »

Hi,

Code: Select all

OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.20.29177 (Git)
Build type: Release
Branch: releases/FreeCAD-0-20
Hash: 68e337670e227889217652ddac593c93b5e8dc94
Python 3.8.10, Qt 5.15.2, Coin 4.0.1, Vtk 8.2.0, OCC 7.6.2
Installed mods: 
  * BIM 2021.12.0
I'd like to know why the line drawing function example shown here https://wiki.freecadweb.org/Line_drawing_function is not working with FC 20 ? Is there something obsolete ?
User avatar
Roy_043
Veteran
Posts: 8537
Joined: Thu Dec 27, 2018 12:28 pm

Re: Line Drawing Function not working

Post by Roy_043 »

The example works here. I have tested the instructions in "Testing the script" and in "Registering the script". Did you restart FreeCAD?

Code: Select all

OS: Windows 8.1 Version 6.3 (Build 9600)
Word size of FreeCAD: 64-bit
Version: 0.20.29177 (Git)
Build type: Release
Branch: releases/FreeCAD-0-20
Hash: 68e337670e227889217652ddac593c93b5e8dc94
Python 3.8.10, Qt 5.15.2, Coin 4.0.1, Vtk 8.2.0, OCC 7.6.2
Locale: Dutch/Netherlands (nl_NL)
Installed mods: 
SBlade
Posts: 40
Joined: Fri May 01, 2020 8:38 pm

Re: Line Drawing Function not working

Post by SBlade »

@Roy_043
Yes it is working, I simply copy pasted the following code into Python Console, that is, I didn't go through testing and registering script. Probably it didn't invoke all commands from pivy and coin frameworks unless registered. Can't we call event handling hooking on run-time?

Code: Select all

import FreeCADGui, Part
from pivy.coin import *

class line:

    """This class will create a line after the user clicked 2 points on the screen"""

    def __init__(self):
        self.view = FreeCADGui.ActiveDocument.ActiveView
        self.stack = []
        self.callback = self.view.addEventCallbackPivy(SoMouseButtonEvent.getClassTypeId(), self.getpoint)

    def getpoint(self, event_cb):
        event = event_cb.getEvent()
        if event.getState() == SoMouseButtonEvent.DOWN:
            pos = event.getPosition()
            point = self.view.getPoint(pos[0], pos[1])
            self.stack.append(point)
            if len(self.stack) == 2:
                l = Part.LineSegment(self.stack[0], self.stack[1])
                shape = l.toShape()
                Part.show(shape)
                self.view.removeEventCallbackPivy(SoMouseButtonEvent.getClassTypeId(), self.callback)
I'd like to further improve that example with providing the ghost tracker when user clicks first point. So that user can see where exactly line is drawn. I located the gui_trackers.py-> ghostTracker class but I don't know how to use it any idea or entry point will be appreciated.
Last edited by SBlade on Sun Jun 26, 2022 3:13 pm, edited 1 time in total.
User avatar
onekk
Veteran
Posts: 6197
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Line Drawing Function not working

Post by onekk »

SBlade wrote: Sun Jun 26, 2022 11:30 am ...

Please use the "</>" when posting code,, and not " icon.

This way the code is readable.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
Roy_043
Veteran
Posts: 8537
Joined: Thu Dec 27, 2018 12:28 pm

Re: Line Drawing Function not working

Post by Roy_043 »

SBlade wrote: Sun Jun 26, 2022 11:30 am Probably it didn't invoke all commands from pivy and coin frameworks unless registered.
Copy-pasting the code also works here. But then you have to use line() instead of exercise.line() to test.
User avatar
onekk
Veteran
Posts: 6197
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Line Drawing Function not working

Post by onekk »

From what I remember

1) there is a sort of persistence, at least in FC "session", so if you define a method (function) in a script you will usually find it available in the Python Console.
2) There are however some settings to add some "isolation level" to the code input and executed as a script in the "Macro Editor", in Preferences >> General >> Macro >> General macro settings - Run macro in local environment that at least in theory should isolate some more the code executed in the "Macro Editor"

You could however add proper line to launch the code in the editor after the code copied, and test here the working.

The whole article you linked create a WorkBench (WB) with the code so it could be activated and used in FC without explicit loading it "Macro Editor"

Edit

Probably I've made a mess mixing infos about Feature Python Objects (FPO), and the code linked is not creating FPO, offending lines are barred below.

There are many ways to make a job, if you want objects in a standard FCStd file, I think the only viable way is to "make a WB" so the code could be found by FC when you open the document containing "custom objects".

But this file is not portable unless the recipient will have the WB installed.



Some old test I've done were not clear at least from what I remember:

The file is open without errors, but some thing could occur:

1) if you try to modify the object it throw an error
2) the actions are not executed due to the lack of proper code.

Anyone know some more detailed infos, maybe with some link to wiki pages where the mechanic is explained?


https://wiki.freecadweb.org/Create_a_Fe ... ect_part_I
https://wiki.freecadweb.org/Create_a_Fe ... ct_part_II

Sorry for the mess.

End Edit

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Post Reply