Some way to isolate/separate body being edited

Have some feature requests, feedback, cool stuff to share, or want to know where FreeCAD is going? This is the place.
Forum rules
Be nice to others! Read the FreeCAD code of conduct!
HoWil
Veteran
Posts: 1279
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Re: Some way to isolate/separate body being edited

Post by HoWil »

DeepSOIC wrote: Mon Mar 29, 2021 4:57 pm My Part-o-magic addon is all about that! Though it's not a separate view.
I used it years ago and can barely remember how it works.... Doesn't it need additional/non-standard FC containers? Does it modify the model so it is no more compatible to standard 0.19 FC?
Is it compatible with current fc versions and is it still developed?
BR
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: Some way to isolate/separate body being edited

Post by DeepSOIC »

HoWil wrote: Wed Mar 31, 2021 5:59 am Doesn't it need additional/non-standard FC containers?
It does provide ones, but it doesn't "need" them, it will affect how Parts and Bodies behave and does not cause any incompatibility, except for actively changing display modes of bodies which may make some people think it makes models incompatible with vanilla freecad. But it doesn't really.
HoWil wrote: Wed Mar 31, 2021 5:59 am Is it compatible with current fc versions and is it still developed?
Mostly compatible, with the exception it may misbehave with Link-related stuff. It is not actively developed, but i'm trying to at least fix big bugs from time to time, because i use it myself.
paulo_cmv
Posts: 15
Joined: Tue Oct 20, 2020 7:43 pm

Re: Some way to isolate/separate body being edited

Post by paulo_cmv »

- Correct some bugs
- Added use info
- Alternatively, bind it to some keyboard shortcut (im using Ctrl+spacebar)

Code: Select all

# Togglevis is a macro created for FreeCAD .19 and is intended to toggle the visibility of selected/non selected objects to make it easer to edit when you have many objects in scene
#
# HOW TO USE: select the objects you want to keep visible (any vertex, edge, face will work) then run the macro. 
# All non selected objects will be hidden. 
# Run the macro again to restore previsous visibility state.
# - Alternatively, bind it to some keyboard shortcut (im using Ctrl+spacebar)
#
__title__    = "ToggleVis.py"
__author__   = "Paulo Rogerio de Oliveira"
__version__  = "1.0"
__date__     = "2021/03/31"    #YYY/MM/DD
__Comment__  = "Hide unselected objects so you can edit then easily. restore prior visibility state running the macro again"
__Help__     = "Select objects you must keep visible and run the macro. Run it again to restore previsous visibility."
__Status__   = "stable"
__Requires__ = "freecad 0.19 (not tested in previous versions)"

import FreeCAD

import PySide
from PySide import QtGui ,QtCore
from PySide.QtGui import *
from PySide.QtCore import *

# clear output window
mw=Gui.getMainWindow()

#	c=mw.findChild(QtGui.QPlainTextEdit, "Python console")
#	c.clear()
#	r=mw.findChild(QtGui.QTextEdit, "Report view")
#	r.clear()

#initialize cache
if not hasattr(FreeCAD, "previouslyVisibleObjects"):
	FreeCAD.previouslyVisibleObjects=[]
if not hasattr(FreeCAD, "previouslyHiddenObjects"):
	FreeCAD.previouslyHiddenObjects=[]

# store all parents of all selected objects to ensure its keep visible (store the Label)
# for parentList and selectedList, store the QTreeWidgetItem reference. Try to avoid error if change label while in edit mode
parentList = []
selectedList = []

# try to get internal object associated with QTreeWidgetItem
def getInternalObject(treeItem):
	# first looks by Label
	obj = App.ActiveDocument.getObjectsByLabel(treeItem.data(0,0))
	if len(obj) > 0:
		return obj[0]
	else:
		# if not found, looks by Name
		return App.ActiveDocument.getObject(treeItem.data(0,0))
	pass
	
# for each object in treeview
def traverse(list, pad):
	# for each child
	for idx in range(list.childCount()):
		item = list.child(idx)
		name = item.data(0, 0)
		obj = getInternalObject(item)

		isParent = name in parentList
		isSelected = name in selectedList

		# hide any object that is visible and not parent of selected or selected itself - store it on list to restore state later
		if obj.Visibility == True and not isParent and not isSelected:
			obj.Visibility = False
			FreeCAD.previouslyVisibleObjects.append(item)

		# if not selected, not a group or if its parent of selected, traverse childs
		if not isSelected: 
			if not hasattr(obj, "Group") or isParent:
				traverse	(item, pad + "  ")
			

# if there are objects in visibility cache, you are in edit mode		
inEdit = len(FreeCAD.previouslyVisibleObjects) > 0
if inEdit:
	FreeCAD.Console.PrintWarning("Leaving edig mode...")
	# show all objects that where visible when entered edit mode
	for object in FreeCAD.previouslyVisibleObjects:
		obj = getInternalObject(object)
		try:
			obj.Visibility = True
		except:
			pass
	# hide all objects that where hidden when entered edit mode
	for object in FreeCAD.previouslyHiddenObjects:
		obj = getInternalObject(object)
		try:
			obj.Visibility = True
		except:
			pass
	# reset visibility cache
	FreeCAD.previouslyVisibleObjects = []
	FreeCAD.previouslyHiddenObjects = []
	#FreeCADGui.SendMsgToActiveView("ViewFit")
else:

	tab = mw.findChild(QtGui.QTabWidget, u'combiTab')
	tree = tab.widget(0).findChildren(QtGui.QTreeWidget)[0]
	top = tree.invisibleRootItem().child(0)

	# get selected objects
	sels = FreeCADGui.Selection.getSelection()
	if not sels:
		FreeCAD.Console.PrintWarning("Select something first!\n\n")
	else:
		FreeCAD.Console.PrintWarning("Entering edit mode!\n\n")

		# find the treeitem for ActiveDocument
		for idx in range(top.childCount()):
			doc 	= top.child(idx)
			if doc.data(0,0) == App.ActiveDocument.Label:
				top = doc

		# store a list of selected items and its parents to keep it visible
		for sel in tree.selectedItems():
			current = sel
			selectedList.append(current.data(0,0))
			while current != None:
				obj = getInternalObject(current)
				if hasattr(obj, "Visibility") and obj.Visibility == False:
					FreeCAD.previouslyHiddenObjects.append(current)
				current = current.parent()
				if current != None:
					parentList.append(current.data(0,0))

		traverse(top, "")
Last edited by paulo_cmv on Thu Apr 01, 2021 1:05 pm, edited 1 time in total.
paulo_cmv
Posts: 15
Joined: Tue Oct 20, 2020 7:43 pm

Re: Some way to isolate/separate body being edited

Post by paulo_cmv »

mario52 wrote: Mon Mar 29, 2021 4:24 pm good macro, when it is finished you must adding in Macro Recipes section 3D View operations with Macro Toggle Visibility (serial)
How can it be made?
How can i ask to add it to Addon Manager / Macros?
Syres
Veteran
Posts: 2893
Joined: Thu Aug 09, 2018 11:14 am

Re: Some way to isolate/separate body being edited

Post by Syres »

paulo_cmv wrote: Wed Mar 31, 2021 2:16 pm

Code: Select all

Rogério de Oliveira"
You'll have a lot of grumpy English lang users, best to remove any accents at this stage!!

Give errors such as:

Code: Select all

15:32:40  <class 'SyntaxError'>: ("(unicode error) 'utf-8' codec can't decode byte 0xe9 in position 9: invalid continuation byte", ('E:/Data/FreeCAD/Macro/EditMode.FCMacro', 9, 16, None))
paulo_cmv
Posts: 15
Joined: Tue Oct 20, 2020 7:43 pm

Re: Some way to isolate/separate body being edited

Post by paulo_cmv »

Ok. Ill have to review this. I copied this section from another macro. Will keep only the how to use...
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: Some way to isolate/separate body being edited

Post by mario52 »

Hi

see here How to get wiki editing permissions

see Macro_documentation

you create your page and not forgote create a link for your wiki page in this page Macros_recipes

thanks for your macro

mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
Brutha
Posts: 221
Joined: Mon May 04, 2015 1:50 pm

Re: Some way to isolate/separate body being edited

Post by Brutha »

paulo_cmv wrote: Wed Mar 31, 2021 2:16 pm - Correct some bugs
- Added use info
- Alternatively, bind it to some keyboard shortcut (im using Ctrl+spacebar)
Thanks for the macro! However, on my system I get the following error when I try and use it:

Code: Select all

Traceback (most recent call last):
  File "/Users/XXXXXX/Library/Preferences/FreeCAD/Macro/ToggleVisibility.FCMacro", line 121, in <module>
    while current != None:
<class 'NotImplementedError'>: operator not implemented.
Strange because I can do the following in the console:

Code: Select all

>>> 1 != 2
True
>>> 1 != 1
False
Here's my system info:

Code: Select all

OS: macOS 10.15
Word size of FreeCAD: 64-bit
Version: 0.20.24612 (Git)
Build type: Release
Branch: master
Hash: f525904c1be10a0f55aa3502151c2c55e5054259
Python version: 3.9.2
Qt version: 5.12.9
Coin version: 4.0.0
OCC version: 7.5.1
Locale: C/Default (C)
Any ideas?

Cheers

Brutha
Syres
Veteran
Posts: 2893
Joined: Thu Aug 09, 2018 11:14 am

Re: Some way to isolate/separate body being edited

Post by Syres »

Brutha wrote: Sat Apr 10, 2021 2:15 pm

Code: Select all

Traceback (most recent call last):
  File "/Users/XXXXXX/Library/Preferences/FreeCAD/Macro/ToggleVisibility.FCMacro", line 121, in <module>
    while current != None:
<class 'NotImplementedError'>: operator not implemented.

Code: Select all

Python version: 3.9.2
Any ideas?
Try replacing the section:

Code: Select all

			while current != None:
				obj = getInternalObject(current)
				if hasattr(obj, "Visibility") and obj.Visibility == False:
					FreeCAD.previouslyHiddenObjects.append(current)
				current = current.parent()
				if current != None:
					parentList.append(current.data(0,0))
with:

Code: Select all

			while current:
				obj = getInternalObject(current)
				if hasattr(obj, "Visibility") and obj.Visibility == False:
					FreeCAD.previouslyHiddenObjects.append(current)
				current = current.parent()
				if current:
					parentList.append(current.data(0,0))
I don't have Python 3.9.x to test with at present but tweak this still works on 3.8.x and is a little more Pythonic IMHO so I'm hoping it gets you up and running.

OS: Windows 7 Version 6.1 (Build 7601: SP 1)
Word size of FreeCAD: 64-bit
Version: 0.20.24673 (Git)
Build type: Release
Branch: master
Hash: e66ed26769ce8affe0a4ed330ff0435f4802f05e
Python version: 3.8.6+
Qt version: 5.15.1
Coin version: 4.0.1
OCC version: 7.5.0
Locale: English/United Kingdom (en_GB)
HoWil
Veteran
Posts: 1279
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Re: Some way to isolate/separate body being edited

Post by HoWil »

DeepSOIC wrote: Wed Mar 31, 2021 12:28 pm
HoWil wrote: Wed Mar 31, 2021 5:59 am Doesn't it need additional/non-standard FC containers?
It does provide ones, but it doesn't "need" them, it will affect how Parts and Bodies behave and does not cause any incompatibility, except for actively changing display modes of bodies which may make some people think it makes models incompatible with vanilla freecad. But it doesn't really.
HoWil wrote: Wed Mar 31, 2021 5:59 am Is it compatible with current fc versions and is it still developed?
Mostly compatible, with the exception it may misbehave with Link-related stuff. It is not actively developed, but i'm trying to at least fix big bugs from time to time, because i use it myself.
Would be nice to see this functionality in main FC.
Post Reply