[Solved] 0.19pre, auto off Draft grid on Sketcher edit mode

A forum for research and development of the user interface of FreeCAD

Moderator: agryson

vocx
Posts: 3249
Joined: Thu Oct 18, 2018 9:18 pm

Re: 0.19pre UI suggestion, auto off Draft grid on Sketcher edit mode

Postby vocx » Tue Feb 18, 2020 12:33 am

triplus wrote:
Mon Feb 17, 2020 9:53 pm
...As if going in a workbench like Arch, people that actually do use Draft grid, there it should stay visible...
Arch is also able to use sketches without problem, so the same issue of having a double grid could occur. For example, Arch Windows normally use sketches to define their geometries. However, since the Draft grid is normally on the floor (XY plane), and the windows are normally in a vertical position (XZ, YZ planes), overlapping of grids will rarely be a problem in this case. But if the user uses Sketches for Arch Walls, then the problem is the same as the one reported here.
As there is already a command available, to control the Draft grid visibility, i feel that it doesn't make much sense to go any further and to introduce some workbench specific show and hide filters...
I think it doesn't hurt to add this function to the Sketcher. As I showed in the previous post, the code only does something if the Snapper class exists. If it doesn't, it means that the user hasn't loaded Draft (or Arch) at all, so there is no need to hide the Draft Grid at all. It only hides it, if it exists.

Code: Select all

if hasattr(FreeCADGui,"Snapper"):
    if FreeCADGui.Snapper.grid:
        if FreeCADGui.Snapper.grid.Visible:
            # Hide
We could even add a checkbox with this option, "Hide Draft grid", in the Sketcher Task panel.

Code: Select all

if hide_check_box():
    if hasattr(FreeCADGui,"Snapper"):
        if FreeCADGui.Snapper.grid:
            if FreeCADGui.Snapper.grid.Visible:
                # Hide
Always add the important information to your posts if you need help.
To support the documentation effort, and code development, your donation is appreciated: paypal.
triplus
Posts: 9272
Joined: Mon Dec 12, 2011 4:45 pm

Re: 0.19pre UI suggestion, auto off Draft grid on Sketcher edit mode

Postby triplus » Tue Feb 18, 2020 12:55 am

vocx wrote:
Tue Feb 18, 2020 12:33 am
Arch is also able to use sketches without problem, so the same issue of having a double grid could occur.
This could be a valid point. In such case i guess the Arch command initiating the sketcher edit mode should be responsible to turn of the Draft grid? Looking from Arch workflow perspective, maybe people involved in such use cases actually do want for the Draft grid to stay visible?
We could even add a checkbox with this option, "Hide Draft grid", in the Sketcher Task panel.
But why only a checkbox for Sketcher and Draft grid, FreeCAD is modular by design, there can be more grids:

https://forum.freecadweb.org/viewtopic.php?t=22296

;)
vocx
Posts: 3249
Joined: Thu Oct 18, 2018 9:18 pm

Re: 0.19pre UI suggestion, auto off Draft grid on Sketcher edit mode

Postby vocx » Tue Feb 18, 2020 2:19 am

triplus wrote:
Tue Feb 18, 2020 12:55 am
This could be a valid point. In such case i guess the Arch command initiating the sketcher edit mode should be responsible to turn of the Draft grid?
As far as I understand, Arch wouldn't control this. The user double clicks the Sketch in the tree view, and it is the Sketcher viewprovider the one that launches the Sketcher edit mode. So the added code needs to be in the Sketcher.
But why only a checkbox for Sketcher and Draft grid, FreeCAD is modular by design, there can be more grids:
Because Draft is a standard workbench provided with FreeCAD by default. The Draft grid is a "core" component of FreeCAD, so to speak. Other grids would be secondary, as long as they aren't integrated into the master branch. (It makes sense to further investigate the grid in Chris_G's post above.)

And yes, FreeCAD is modular, but we should also make the workbenches more aware of each other so that the overall experience is better.

One criticism of FreeCAD is that it is seemingly composed of separate pieces, and doesn't look very well planned with a single user interface in mind... and that's exactly the truth! Wondering about the overall UI

We should definitely try to integrate the pieces better so that the overall experience is more uniform. This is a good thing.
Always add the important information to your posts if you need help.
To support the documentation effort, and code development, your donation is appreciated: paypal.
triplus
Posts: 9272
Joined: Mon Dec 12, 2011 4:45 pm

Re: 0.19pre UI suggestion, auto off Draft grid on Sketcher edit mode

Postby triplus » Tue Feb 18, 2020 11:03 am

No, i don't feel adding a Draft grid checkbox in Sketcher task panel is a good idea. But that is my opinion and feel feel to go after such endeavor.

P.S. Best if you get reasonable amount of consensus before doing that.
vocx
Posts: 3249
Joined: Thu Oct 18, 2018 9:18 pm

Re: 0.19pre UI suggestion, auto off Draft grid on Sketcher edit mode

Postby vocx » Tue Feb 18, 2020 4:14 pm

DeepSOIC wrote: ping
I wonder if what is discussed in this thread can be implemented with TempoVis.

I currently see that TempoVis is used in ViewProviderSketch.cpp to save and restore views.

Would it be possible to add the necessary code to turn off the Draft grid when entering the edit mode of a Sketch?

I quickly did a hack on TaskDlgEditSketch.cpp, and it works, but it obviously crashes the system if the Snapper class is not initialized.

Code: Select all

void TaskDlgEditSketch::open()
{
    std::string document = getDocumentName();
    Gui::Command::doCommand(Gui::Command::Gui,
        "Gui.Snapper.grid.off()", document.c_str());
}
---

Edit: this seems to work in ViewProviderSketch.cpp.

In ViewProviderSketch::setEdit.

Code: Select all

try{
            QString cmdstr = QString::fromLatin1(
                        "ActiveSketch = App.getDocument('%1').getObject('%2')\n"
                        "tv = Show.TempoVis(App.ActiveDocument, tag= ActiveSketch.ViewObject.TypeId)\n"
                        "ActiveSketch.ViewObject.TempoVis = tv\n"
                        "if ActiveSketch.ViewObject.EditingWorkbench:\n"
                        "  tv.activateWorkbench(ActiveSketch.ViewObject.EditingWorkbench)\n"
                        "if ActiveSketch.ViewObject.HideDependent:\n"
                        "  tv.hide(tv.get_all_dependent(%3, '%4'))\n"
                        "if ActiveSketch.ViewObject.ShowSupport:\n"
                        "  tv.show([ref[0] for ref in ActiveSketch.Support if not ref[0].isDerivedFrom(\"PartDesign::Plane\")])\n"
                        "if ActiveSketch.ViewObject.ShowLinks:\n"
                        "  tv.show([ref[0] for ref in ActiveSketch.ExternalGeometry])\n"
                        "tv.hide(ActiveSketch)\n"
                        "if hasattr(FreeCADGui, 'Snapper'):\n"
                        "  if FreeCADGui.Snapper.grid:\n"
                        "    if FreeCADGui.Snapper.grid.Visible:\n"
                        "      FreeCADGui.Snapper.grid.off()\n"
                        "      FreeCADGui.Snapper.forceGridOff=True\n"
                        "del(tv)\n"
                        ).arg(QString::fromLatin1(getDocument()->getDocument()->getName()),
                              QString::fromLatin1(getSketchObject()->getNameInDocument()),
                              QString::fromLatin1(Gui::Command::getObjectCmd(editObj).c_str()),
                              QString::fromLatin1(editSubName.c_str()));
            QByteArray cmdstr_bytearray = cmdstr.toLatin1();
            Gui::Command::runCommand(Gui::Command::Gui, cmdstr_bytearray);
        }
        ...
Then to restore, ViewProviderSketch::unsetEdit.

Code: Select all

try{
        QString cmdstr = QString::fromLatin1(
                    "ActiveSketch = App.getDocument('%1').getObject('%2')\n"
                    "tv = ActiveSketch.ViewObject.TempoVis\n"
                    "if tv:\n"
                    "  tv.restore()\n"
                    "ActiveSketch.ViewObject.TempoVis = None\n"
                    "if hasattr(FreeCADGui,'Snapper'):\n"
                    "  if FreeCADGui.Snapper.grid:\n"
                    "    FreeCADGui.Snapper.grid.on()\n"
                    "    FreeCADGui.Snapper.forceGridOff=False\n"
                    "del(tv)\n"
                    ).arg(QString::fromLatin1(getDocument()->getDocument()->getName())).arg(
                          QString::fromLatin1(getSketchObject()->getNameInDocument()));
        QByteArray cmdstr_bytearray = cmdstr.toLatin1();
        Gui::Command::runCommand(Gui::Command::Gui, cmdstr_bytearray);
    }
Always add the important information to your posts if you need help.
To support the documentation effort, and code development, your donation is appreciated: paypal.
fc_tofu
Posts: 254
Joined: Sun Jan 05, 2020 4:56 pm

Re: 0.19pre UI suggestion, auto off Draft grid on Sketcher edit mode

Postby fc_tofu » Wed Feb 19, 2020 6:38 pm

vocx wrote:
Mon Feb 17, 2020 5:45 pm
The code that turns on and off Draft's grid is a class called the Snapper. This class is initialized when we first switch to the Draft Workbench. Then the Snapper lives inside the FreeCADGui namespace.

This means that Sketcher's code needs to be augmented in the following way. When we enter the edit mode of the Sketch, we need to test for the Snapper, and turn off the grid.

Code: Select all

if hasattr(FreeCADGui,"Snapper"):
    if FreeCADGui.Snapper.grid:
        if FreeCADGui.Snapper.grid.Visible:
            FreeCADGui.Snapper.grid.off()
            FreeCADGui.Snapper.forceGridOff=True
When we exit the Sketch, we could do the opposite and re-activate it.

Code: Select all

if hasattr(FreeCADGui,"Snapper"):
    if FreeCADGui.Snapper.grid:
        FreeCADGui.Snapper.grid.on()
        FreeCADGui.Snapper.forceGridOff=False
Sorry, when I recheck your mockup code, I find the latter paragraph cannot restore draft grid status in an intelligent way.
Say, if Draft grid is off before entering Sketcher edit mode, your latter paragraph still turn on Draft grid when exiting Sketcher edit mode.

Could you add "restore previous Draft grid status" function?
User avatar
DeepSOIC
Posts: 7469
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: 0.19pre UI suggestion, auto off Draft grid on Sketcher edit mode

Postby DeepSOIC » Thu Feb 27, 2020 11:19 am

vocx wrote:
Tue Feb 18, 2020 4:14 pm
… <disable draft grid from sketcher code>
It is possible, of course. IMO, it is not the right solution - at least, Werner will call it spaghetti code and will probably not accept it. I can think of three ways of solving it.
1) Draft workbench should hide its grid when deactivated.
Simple, easy, no cross-module dependency. But with some caveats, obviously: sometimes, one might want the grid to stay.
2) In Draft wb, add an observer that senses when an object enters edit mode, and if it's not Draft object, turn off the grid.
3) add a centralized App-based grid feature (or, maybe, Show-based?), and make Draft and Sketcher use it, instead of their own implementations. Then, the conflict can be resolved nicely, without Draft having to know about Sketcher and Sketcher having to know about Draft.
carlopav
Posts: 693
Joined: Mon Dec 31, 2018 1:49 pm

Re: 0.19pre UI suggestion, auto off Draft grid on Sketcher edit mode

Postby carlopav » Thu Feb 27, 2020 3:23 pm

DeepSOIC wrote:
Thu Feb 27, 2020 11:19 am
1) Draft workbench should hide its grid when deactivated.
Simple, easy, no cross-module dependency. But with some caveats, obviously: sometimes, one might want the grid to stay.
2) In Draft wb, add an observer that senses when an object enters edit mode, and if it's not Draft object, turn off the grid.
3) add a centralized App-based grid feature (or, maybe, Show-based?), and make Draft and Sketcher use it, instead of their own implementations. Then, the conflict can be resolved nicely, without Draft having to know about Sketcher and Sketcher having to know about Draft.
Sorry, I heard spaghetti and i could't help but stepping in :lol:
n. 3 sounds the most desirable to me, but if it need too much effort we can just go for the n. 1 and add a preference to prevent switching off the grid on module deactivation.
fc_tofu
Posts: 254
Joined: Sun Jan 05, 2020 4:56 pm

Re: 0.19pre UI suggestion, auto off Draft grid on Sketcher edit mode

Postby fc_tofu » Thu Feb 27, 2020 5:59 pm

DeepSOIC wrote:
Thu Feb 27, 2020 11:19 am
vocx wrote:
Tue Feb 18, 2020 4:14 pm
… <disable draft grid from sketcher code>
It is possible, of course. IMO, it is not the right solution - at least, Werner will call it spaghetti code and will probably not accept it. I can think of three ways of solving it.
1) Draft workbench should hide its grid when deactivated.
Simple, easy, no cross-module dependency. But with some caveats, obviously: sometimes, one might want the grid to stay.
2) In Draft wb, add an observer that senses when an object enters edit mode, and if it's not Draft object, turn off the grid.
3) add a centralized App-based grid feature (or, maybe, Show-based?), and make Draft and Sketcher use it, instead of their own implementations. Then, the conflict can be resolved nicely, without Draft having to know about Sketcher and Sketcher having to know about Draft.
As for no.2, is it possible to restore previous Draft grid status (if Draft grid was on) when exit Sketcher edit mode? Though it is not a must.

As for no.3, I am thinking, why Sketcher developer decided not use Draft grid directly, if as vox said, Draft grid is "core" function of FreeCAD. The possible reason might be Draft grid ony cover a limited size (predefined in pref), which is not suitable to work on ortho view. If such guess is true, we can learn from Blender's grid.
Blender grid treat tilted/ortho view in different way, it convers limited square in tilted 3d view, but covers unlimited area in ortho view.
Since Draft grid in used by more workbenches, to keep Draft grid (and give up Sketcher grid) as the only/solely/centralized/unified FreeCAD grid is a bargain. To implement this, we need test,
1. replace Sketcher grid with Draft grid in Sketcher edit mode.
2. add Draft grid a same ability like Blender grid (automatically change size in different view), which feature is also quite useful in all other workbenches.

So much work is waiting for coders to do before 0.19 release. This is not urgent.
vocx
Posts: 3249
Joined: Thu Oct 18, 2018 9:18 pm

Re: 0.19pre UI suggestion, auto off Draft grid on Sketcher edit mode

Postby vocx » Thu Feb 27, 2020 7:12 pm

DeepSOIC wrote:
Thu Feb 27, 2020 11:19 am
1) Draft workbench should hide its grid when deactivated.
Simple, easy, no cross-module dependency. But with some caveats, obviously: sometimes, one might want the grid to stay.
Yes, I like to have the Draft grid active, even if working in other workbenches. It provides a nice visual reference.

Also, you can add a sketch in Draft and Arch, and double click it to start its edit mode, so you aren't technically de-activating the Draft workbench, you are just moving to Sketcher, and coming back from it. Or is that considered De-activating Draft?
2) In Draft wb, add an observer that senses when an object enters edit mode, and if it's not Draft object, turn off the grid.
How is this implemented? Is the edit mode not defined by each object? That is, Sketches define their own edit mode, just like Draft objects define their own edit mode.

What is the purpose of TempoVis exactly? It seems precisely something that "saves" a view, then restores it afterwards. Is it meant to be used only in Sketcher?
3) add a centralized App-based grid feature (or, maybe, Show-based?), and make Draft and Sketcher use it, instead of their own implementations. Then, the conflict can be resolved nicely, without Draft having to know about Sketcher and Sketcher having to know about Draft.
What is Show exactly? Is it a module to control what? I could investigate this more, but there is some serious lack of documentation.

A global grid is the most sensible approach, I think, the problem is who is going to implement it, and how.

Draft's grid's is essentially a global grid, made in Coin. However, it is placed in Draft for some reason. Maybe it could be moved to a global namespace?

On the other hand, the Sketcher grid is embedded into every sketch. Each Sketch has its own internal grid. I think this makes sense, because the shapes contained inside are referred to their own coordinate system. For example, how would it work if you move a sketch to somewhere else? Say you attach it to a plane, and then want to modify the sketch. The global grid would have to be moved to this reference as well. When the sketch uses its own grid, its internal grid is moved at the same time when you change the attachment.

The Sketch is based on the Part_Part2DObject, which has the Part::AttachExtension. The grid is defined in its view provider, PartGui::ViewProvider2DObject.

https://github.com/FreeCAD/FreeCAD/blob ... 2DObject.h
Always add the important information to your posts if you need help.
To support the documentation effort, and code development, your donation is appreciated: paypal.