Spreadsheet alias access

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
donjan
Posts: 33
Joined: Wed Jun 21, 2017 11:40 am

Spreadsheet alias access

Post by donjan »

OS: Ubuntu 17.04
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.17.11528 (Git)
Build type: None
Branch: master
Hash: d31be3117de386b4f6cd3444e0b330db56dc6ba2
Python version: 2.7.13
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 7.1.0

In the attached minimal example, there's a spreadsheet, a simple sketch and an extrude thereof, modelling a wire.
The extrude takes for its length a cell value from the spreadsheet.

1) Alias access in spreadsheet
To store the wire length, set an alias of a spreadsheet cell, e.g. C3, to 'wire_length'. For convenience, the same name should be visible in front: the cell B3 should hold something like =C3.alias.
I can do it manually, but there will be about three dozen parameters, so I'm not looking forward to writing every alias string twice.
Ideas for an efficient workflow?
[edit: I'm fine with scripting... if there's something as described as a console one- or three-liner, that'd be awesome]

2) Alias access as property
In the Combo view, I can select the sketch and edit it's properties directly right there. In particular the one constraint it has, which corresponds to the wire thickness.
I can select the spreadsheet, and wire_length is tauntingly shown as a property, but it's not editable.
I think this would be a great quality of life improvement. (the wire_thickness will then go into the spreadsheet as well)
Attachments
spread.fcstd
(6.87 KiB) Downloaded 64 times
donjan
Posts: 33
Joined: Wed Jun 21, 2017 11:40 am

Re: Spreadsheet alias access

Post by donjan »

Self do, self have ;)
For the first question I came up with this. Seems sufficient for now.

Code: Select all

ss = App.ActiveDocument.Spreadsheet  # spreadsheet name is hardcoded for now
for a in range(ord('B'), ord('Z')+1):
  for i in range(1, 50):
    cellalias = ss.getAlias(chr(a) + str(i))
    if cellalias:  # write alias string to left cell
      ss.set(chr(a-1) + str(i), '\'' + cellalias);

App.ActiveDocument.recompute()
edit: added as FCMacro
Attachments
spreadsheet_alias_to_left.FCMacro.zip
(473 Bytes) Downloaded 60 times
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Spreadsheet alias access

Post by microelly2 »

You can define your spreadsheet as a Spreadsheet::SheetPython and add the methods and properties you need

To start ...

Code: Select all

'''python objects for freecad'''

# -*- coding: utf-8 -*-
#-- microelly 2017 v 0.1
#-- GNU Lesser General Public License (LGPL)


##\cond
import FreeCAD
import FreeCADGui
App = FreeCAD
Gui = FreeCADGui

import Part
import numpy as np


class FeaturePython:
	''' basic defs'''

	def __init__(self, obj):
		obj.Proxy = self
		self.Object = obj

	def attach(self, vobj):
		self.Object = vobj.Object

	def claimChildren(self):
		return self.Object.Group

	def __getstate__(self):
		return None

	def __setstate__(self, state):
		return None


class ViewProvider:
	''' basic defs '''

	def __init__(self, obj):
		obj.Proxy = self
		self.Object = obj

	def __getstate__(self):
		return None

	def __setstate__(self, state):
		return None
##\endcond


class MyTable(FeaturePython):
	'''spreadsheet Object with Python''' 

	def __init__(self, obj):
		obj.Proxy = self
		self.Type = self.__class__.__name__
		ViewProvider(obj.ViewObject) 

	def onBeforeChange(proxy,obj,prop):
		print "huhu"



obj = FreeCAD.ActiveDocument.addObject('Spreadsheet::SheetPython','name')

obj.addProperty("App::PropertyLink", "base", "Base",)
obj.addProperty("App::PropertyBool", "off", "Base",)
obj.addProperty("App::PropertyIntegerList", "relation", "Base",)
obj.addProperty("App::PropertyVector", "offset", "Base",)



MyTable(obj)

HoWil
Veteran
Posts: 1279
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Re: Spreadsheet alias access

Post by HoWil »

donjan wrote: Tue Jul 11, 2017 6:42 pm Self do, self have ;)
For the first question I came up with this. Seems sufficient for now.

Code: Select all

ss = App.ActiveDocument.Spreadsheet  # spreadsheet name is hardcoded for now
for a in range(ord('B'), ord('Z')+1):
  for i in range(1, 50):
    cellalias = ss.getAlias(chr(a) + str(i))
    if cellalias:  # write alias string to left cell
      ss.set(chr(a-1) + str(i), '\'' + cellalias);

App.ActiveDocument.recompute()
edit: added as FCMacro
Nice.

If you are willing to create a more usefull macro for GUI users you could add it to the following macro which allows copying parts of a spreadsheet with a gui (see the screencast fist):
https://forum.freecadweb.org/viewtopic. ... et#p164461

BR,
HoWil
donjan
Posts: 33
Joined: Wed Jun 21, 2017 11:40 am

Re: Spreadsheet alias access

Post by donjan »

microelly2 wrote: Tue Jul 11, 2017 7:09 pm You can define your spreadsheet as a Spreadsheet::SheetPython and add the methods and properties you need

To start ...
Thanks, adding properties is a good idea.
I can just take my snippet from above and say

Code: Select all

ss.addProperty("App::PropertyBool", "off", "Base",)
The main issue is that the table cells are not editable, while properties added manually are.

Does anybody know how to monkey patch the spreadsheet object to enable editing from the combo view?

HoWil wrote: Tue Jul 11, 2017 8:07 pm Nice.

If you are willing to create a more usefull macro for GUI users you could add it to the following macro which allows copying parts of a spreadsheet with a gui (see the screencast fist):
https://forum.freecadweb.org/viewtopic. ... et#p164461

BR,
HoWil
Well, one major issue is that I bluntly iterate over an arbitrary section of the table, which is sufficient for my personal needs.
A less crazy and much more efficient method would be to read only what is in App.ActiveDocument.Spreadsheet.cells.Content, but I really don't feel like parsing XML and can't discover a Python-list version of those contents in the refs (to be fair I wasn't looking that hard).

So no, I'm not looking to spend substantial amounts of time on polishing this, which is why I was asking for a 1-3 liner originally ;)
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Spreadsheet alias access

Post by microelly2 »

donjan wrote: Tue Jul 11, 2017 8:27 pm
microelly2 wrote: Tue Jul 11, 2017 7:09 pm You can define your spreadsheet as a Spreadsheet::SheetPython and add the methods and properties you need

To start ...
Thanks, adding properties is a good idea.
I can just take my snippet from above and say

Code: Select all

ss.addProperty("App::PropertyBool", "off", "Base",)
The main issue is that the table cells are not editable, while properties added manually are.

Does anybody know how to monkey patch the spreadsheet object to enable editing from the combo view?
If you look into the fine structure of the Qt Widget for the spreadsheet you will find possibities to exchange data between spreadsheet and other widgets/properties

I have done some work to get a good interaction between spreadsheet and model

It should be possible to transfer the ideas to your needs.

https://www.youtube.com/watch?v=f-6I7SRBDvs
https://youtu.be/jxyuS9JiYTQ

the scripts are in my nurbs workbench
https://github.com/microelly2/freecad-nurbs

I will extract a small example for your needs next days (it's to late this evening (for me) ;))
donjan
Posts: 33
Joined: Wed Jun 21, 2017 11:40 am

Re: Spreadsheet alias access

Post by donjan »

Looks neat. A clean simple extract would be nice, thanks!

For now, I don't use the property window editing: I'd have to add the property via console, then add =propertyname to a cell, then add 'propertyname to the left neighbour cell, for each property. Sure, part of that can be automated, but the goal is to have it be convenient.

How do you get those Qt window views? I'm on GTK+Compiz (no Unity) and can pick View -> Document window -> Undocked, which takes the first view out of the app, and subsequent views as in-app windows (Qt titlebar with min,max,close) as in your video, but I can't manage to get all of them inside. That's on yesterdays 0.17 daily.
edit: nevermind, Windows -> Tile

Schönen Tag noch.
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Spreadsheet alias access

Post by microelly2 »

donjan wrote: Wed Jul 12, 2017 9:31 am Looks neat. A clean simple extract would be nice, thanks!
Here the idea how to access the cells of the spreadsheet by mouse events

Code: Select all



class MeineTabelle:


	def clicked(self,index):
		print "Clicked",index
		print ("dumpix", index.row(),index.column(),index.data())

	def entered(self,index):
		print "Entered"
		print ("dumpix", index.row(),index.column(),index.data())

	def pressed(self,index):
		print "Pressed"
		print ("dumpix", index.row(),index.column(),index.data())


	def startssevents(self):

		mw=FreeCADGui.getMainWindow()
		mdiarea=mw.findChild(QtGui.QMdiArea)

		App.activeDocument().SpreadsheetABC.ViewObject.startEditing(0)
		subw=mdiarea.subWindowList()
		for i in subw:
			if i.widget().metaObject().className() == "SpreadsheetGui::SheetView":
				sheet = i.widget()
				table=sheet.findChild(QtGui.QTableView)

		table.clicked.connect(self.clicked)
		table.entered.connect(self.entered)
		table.pressed.connect(self.pressed)
		self.table=table




obj=App.activeDocument().addObject('Spreadsheet::Sheet','SpreadsheetABC')
obj.addProperty("App::PropertyString", "MeinParam", "Base",)

# in die zellen schreiben
obj.set('A9', '23')
obj.set("B4","45")

#zugriffe auswerten 
mt=MeineTabelle()
mt.startssevents()

after running the script in a new doc you can click the cells A9,B4 and will get feedback in the message log.
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: Spreadsheet alias access

Post by DeepSOIC »

Post Reply