Anyway to highlight the assembly constraints?

Discussion about the development of the Assembly workbench.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
ceremcem
Posts: 226
Joined: Sun Jan 07, 2018 11:10 am

Anyway to highlight the assembly constraints?

Post by ceremcem »

It would be useful if we could visually inspect the assembly constraints like we do for sketch constraints:
Untitled.png
Untitled.png (27.37 KiB) Viewed 2712 times
---

Do we currently have such a possibility?
User avatar
HarryGeier
Veteran
Posts: 1231
Joined: Mon Jul 10, 2017 12:36 pm
Location: Hof Germany

Re: Anyway to highlight the assembly constraints?

Post by HarryGeier »

Not that i know ..in Assembly2 . If you are much into assemblies.. maybe hook into the assembly3 thread, which seems to be the future of assembly in FreeCAD. Assembly2 is not actively beeing developed.
https://forum.freecadweb.org/viewtopic. ... &start=300
Kaum macht man´s richtig , gehts´s
My Video Tutorials on Youtube: https://www.youtube.com/channel/UCoe3B ... p8Q/videos
My FreeCAD Stuff on Hidrive: https://my.hidrive.com/share/qr3l1yddy6#$/
kbwbe
Veteran
Posts: 1052
Joined: Tue Apr 10, 2018 3:12 pm
Location: Germany, near Köln (Cologne)

Re: Anyway to highlight the assembly constraints?

Post by kbwbe »

Hello ceremcem,

i think it is simple to modify assembly2 by yourself.
I am already working with an modified version, which i did last time.

At short: You have to extend freecad a little bit. I created a new command with i copied to importpart.py, which is located in your mod-folder and there within the assembly2 folder.

here is the code for the command-extension:

Code: Select all

class ViewConnectionsCommand:
    def Activated(self):
        selection = [s for s in FreeCADGui.Selection.getSelection() if s.Document == FreeCAD.ActiveDocument ]
        if len(selection) == 0: return
        FreeCADGui.Selection.clearSelection()
        
        doc = FreeCAD.ActiveDocument
        connectionToView = selection[0]
        
        if not 'ConstraintInfo' in connectionToView.Content and not 'ConstraintNfo' in connectionToView.Content: 
            return # Selected Object is not a connection/constraint
        
        # Save Transparency of Objects and make all transparent
        transparencyList = []
        for obj in doc.Objects:
            if hasattr(obj,'ViewObject'):
                if hasattr(obj.ViewObject,'Transparency'):
                    transparencyList.append(obj.ViewObject.Transparency) 
                    obj.ViewObject.Transparency = 80 
        
        FreeCADGui.Selection.addSelection(
            doc.getObject(connectionToView.Object1),
            connectionToView.SubElement1
            )
        FreeCADGui.Selection.addSelection(
            doc.getObject(connectionToView.Object2),
            connectionToView.SubElement2
            )
        
        #FreeCADGui.SendMsgToActiveView("ViewSelection") # not good enough
        
        flags = QtGui.QMessageBox.StandardButton.Yes
        msg = "Close Connection ViewMode ?"
        response = QtGui.QMessageBox.information(QtGui.qApp.activeWindow(), "Highlighting ready", msg, flags )
        
        # restore transparency of objects...
        i=0
        for obj in doc.Objects:
            if hasattr(obj,'ViewObject'):
                if hasattr(obj.ViewObject,'Transparency'):
                    obj.ViewObject.Transparency = transparencyList[i]
                    i+=1
        
        FreeCADGui.Selection.clearSelection()
        FreeCADGui.Selection.addSelection(selection[0])

    def GetResources(self):
        return {
            'Pixmap'  : a3lib.pathOfModule()+'/icons/a3_viewConnection.svg',
            'MenuText': 'show connected elements',
            'ToolTip': 'show connected elements'
            }
        
FreeCADGui.addCommand('a3_ViewConnectionsCommand', ViewConnectionsCommand())
 
The command turns all parts of the assembly to transparency and highlights the components of an selected constraint.
I dont have much time at the moment and have to leave the house within a few minutes, shortly i will tell you more...

p.s.: PLEASE PROVIDE YOUR SYSTEMINFORMATION...

regards,
Klaus
KBWBE

https://github.com/kbwbe/A2plus
latest release: v0.4.56, installable via FreeCAD's addon manager
Tutorial: gripper assembly https://www.youtube.com/watch?v=QMxcQ5tssWk
Documentation: https://www.freecadweb.org/wiki/A2plus_Workbench
Koemi
Posts: 150
Joined: Thu Dec 28, 2017 11:13 am
Location: The Netherlands

Re: Anyway to highlight the assembly constraints?

Post by Koemi »

Helle kbwbe,

I am also intested in this 'highlighting'-feature.

At 1st I copied the importpart.py (just in case)...
I tried your modification (I added your code to the importpart.py)


I restarted Freecad and tried assembly2.. And I got this message..?
Image


Actually: there IS a way to show constraints (but it takes a few clicks)
https://forum.freecadweb.org/viewtopic.php?f=22&t=26546


OS: Windows 10
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.16.6712 (Git)
Build type: Release
Branch: releases/FreeCAD-0-16
Hash: da2d364457257a7a8c6fb2137cea12c45becd71a
Python version: 2.7.8
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 6.8.0.oce-0.17
kbwbe
Veteran
Posts: 1052
Joined: Tue Apr 10, 2018 3:12 pm
Location: Germany, near Köln (Cologne)

Re: Anyway to highlight the assembly constraints?

Post by kbwbe »

Hello Koemi,

sorry, i could finish my complete explanation yesterday because lack of time.
From your system-information i see that you are using windows. (i am using linux only, do not have a windows machine)

Therefore i adapted my script which you have to copy to importpart.py a little bit and removed my icon. (which can not bee found in your system)

Here is the new code, but please follow the copy instructions afterwards...

Code: Select all


class ViewConnectionsCommand:
    def Activated(self):
        selection = [s for s in FreeCADGui.Selection.getSelection() if s.Document == FreeCAD.ActiveDocument ]
        if len(selection) == 0: return
        FreeCADGui.Selection.clearSelection()
        
        doc = FreeCAD.ActiveDocument
        connectionToView = selection[0]
        
        if not 'ConstraintInfo' in connectionToView.Content and not 'ConstraintNfo' in connectionToView.Content: 
            return # Selected Object is not a connection/constraint
        
        # Save Transparency of Objects and make all transparent
        transparencyList = []
        for obj in doc.Objects:
            if hasattr(obj,'ViewObject'):
                if hasattr(obj.ViewObject,'Transparency'):
                    transparencyList.append(obj.ViewObject.Transparency) 
                    obj.ViewObject.Transparency = 80 
        
        FreeCADGui.Selection.addSelection(
            doc.getObject(connectionToView.Object1),
            connectionToView.SubElement1
            )
        FreeCADGui.Selection.addSelection(
            doc.getObject(connectionToView.Object2),
            connectionToView.SubElement2
            )
        
        #FreeCADGui.SendMsgToActiveView("ViewSelection") # not good enough
        
        flags = QtGui.QMessageBox.StandardButton.Yes
        msg = "Close Connection ViewMode ?"
        response = QtGui.QMessageBox.information(QtGui.qApp.activeWindow(), "Highlighting ready", msg, flags )
        
        # restore transparency of objects...
        i=0
        for obj in doc.Objects:
            if hasattr(obj,'ViewObject'):
                if hasattr(obj.ViewObject,'Transparency'):
                    obj.ViewObject.Transparency = transparencyList[i]
                    i+=1
        
        FreeCADGui.Selection.clearSelection()
        FreeCADGui.Selection.addSelection(selection[0])

    def GetResources(self):
        return {
            #'Pixmap'  : a3lib.pathOfModule()+'/icons/a3_viewConnection.svg',
            'MenuText': 'show constraint',
            'ToolTip': 'show constraint'
            }
        
FreeCADGui.addCommand('a3_ViewConnectionsCommand', ViewConnectionsCommand())

The errormessage you have got is simple. The programming language python is very sensitive to identation of the codelines.
I think somethings got wrong with copying and inserting of my codefragment. Or the place was a wrong one.

I installed assembly2 again and copied the code exactly before this place in importpart.py (within a few empty lines):

Code: Select all

if __name__ == '__main__':
Because Python is so sensitive to identations, the modified importpart.py file has to look finally like:

Code: Select all


		    #a lot of code above...

                    debugPrint(3,'  leaving %s.%s as is, since subElement in old and new shape are equal' % (c.Name, SubElement))


class ViewConnectionsCommand:
    def Activated(self):
        selection = [s for s in FreeCADGui.Selection.getSelection() if s.Document == FreeCAD.ActiveDocument ]
        if len(selection) == 0: return
        FreeCADGui.Selection.clearSelection()
        
        doc = FreeCAD.ActiveDocument
        connectionToView = selection[0]
        
        if not 'ConstraintInfo' in connectionToView.Content and not 'ConstraintNfo' in connectionToView.Content: 
            return # Selected Object is not a connection/constraint
        
        # Save Transparency of Objects and make all transparent
        transparencyList = []
        for obj in doc.Objects:
            if hasattr(obj,'ViewObject'):
                if hasattr(obj.ViewObject,'Transparency'):
                    transparencyList.append(obj.ViewObject.Transparency) 
                    obj.ViewObject.Transparency = 80 
        
        FreeCADGui.Selection.addSelection(
            doc.getObject(connectionToView.Object1),
            connectionToView.SubElement1
            )
        FreeCADGui.Selection.addSelection(
            doc.getObject(connectionToView.Object2),
            connectionToView.SubElement2
            )
        
        #FreeCADGui.SendMsgToActiveView("ViewSelection") # not good enough
        
        flags = QtGui.QMessageBox.StandardButton.Yes
        msg = "Close Connection ViewMode ?"
        response = QtGui.QMessageBox.information(QtGui.qApp.activeWindow(), "Highlighting ready", msg, flags )
        
        # restore transparency of objects...
        i=0
        for obj in doc.Objects:
            if hasattr(obj,'ViewObject'):
                if hasattr(obj.ViewObject,'Transparency'):
                    obj.ViewObject.Transparency = transparencyList[i]
                    i+=1
        
        FreeCADGui.Selection.clearSelection()
        FreeCADGui.Selection.addSelection(selection[0])

    def GetResources(self):
        return {
            #'Pixmap'  : a3lib.pathOfModule()+'/icons/a3_viewConnection.svg',
            'MenuText': 'show constraint',
            'ToolTip': 'show constraint'
            }
        
FreeCADGui.addCommand('a3_ViewConnectionsCommand', ViewConnectionsCommand())
 





if __name__ == '__main__':
    print('\nTesting importPart.py')
    def test_split_and_join( pathLib, path):
        print('Testing splitting and rejoining. lib %s' % (str(pathLib)))
        parts = path_split( pathLib, path )
        print('  parts %s' % parts )
        print('  rejoined   %s'  %  path_join(pathLib, parts ) )
        print('  original   %s'  %  path )

    test_split_and_join( ntpath, 'C:/Users/gyb/Desktop/Circular Saw Jig\Side support V1.00.FCStd')
    test_split_and_join( ntpath, 'C:/Users/gyb/Desktop/Circular Saw Jig/Side support V1.00.FCStd')
    test_split_and_join( posixpath, '/temp/hello1/foo.FCStd')

    def test_path_convert( path, pathLibFrom, pathLibTo):
        print('Testing path_convert_lib.')
        print('  original    %s'  %  path )
        converted = path_convert( path, pathLibFrom, pathLibTo)
        print('  converted   %s'  %  converted )
        print('  reversed    %s'  % path_convert( path, pathLibTo, pathLibFrom) )

    test_path_convert( r'C:\Users\gyb\Desktop\Circular Saw Jig\Side support V1.00.FCStd', ntpath, os.path )

This modification only implements the new command, but it is not automaticaly accessible. You need an icon or button in the toolbar of assembly2.
Because I deactivated my icon, you will get a button in the toolbar if you add one line and a comma-separator to the file InitGui.py (i marked the changes within the code).
Please find it here:

Code: Select all


import assembly2lib #QtCore.QResource.registerResource happens in assembly2lib

class Assembly2Workbench (Workbench): 
    MenuText = 'Assembly 2'
    def Initialize(self):
        import axialConstraint, assembly2solver, importPart, planeConstraint, circularEdgeConstraint, muxAssembly, angleConstraint, partsList, degreesOfFreedomAnimation, \
               sphericalSurfaceConstraint, checkAssembly, boltMultipleCircularEdges, animate_constraint, undo
        commandslist = [
            'importPart', 
            'updateImportedPartsCommand', 
            'assembly2_movePart', 
            'addCircularEdgeConstraint', 
            'addPlaneConstraint', 
            'addAxialConstraint', 
            'addAngleConstraint', 
            'addSphericalSurfaceConstraint',
            'a2_UndoConstraint',
            'degreesOfFreedomAnimation', 
            'assembly2SolveConstraints',
            'muxAssembly',
            'muxAssemblyRefresh',
            'addPartsList',
            'assembly2_checkAssembly',          #================== add comma-separator here ======================
            'a3_ViewConnectionsCommand'     #===============> this line is to bee added ==========================
            ]
        self.appendToolbar('Assembly 2', commandslist)
        shortcut_commandslist = [
            'flipLastConstraintsDirection',
            'lockLastConstraintsRotation',
            'boltMultipleCircularEdges',
            ]
        self.appendToolbar('Assembly 2 shortcuts', shortcut_commandslist )
        self.treecmdList = ['importPart', 'updateImportedPartsCommand']
        FreeCADGui.addIconPath( ':/assembly2/icons' )
        FreeCADGui.addPreferencePage( ':/assembly2/ui/assembly2_prefs.ui','Assembly2' )
        self.appendMenu('Assembly 2', commandslist)

    def Activated(self):
        from assembly2lib import FreeCAD, updateOldStyleConstraintProperties
        import os, undo
        doc = FreeCAD.activeDocument()
        if hasattr(doc, 'Objects'):
            updateOldStyleConstraintProperties(doc)
        __dir2__ = os.path.dirname(undo.__file__)
        GuiPath = os.path.expanduser ("~") # os.path.join( __dir2__, 'Gui' )
        constraintFile = os.path.join( GuiPath , 'constraintFile.txt')
        if os.path.exists(constraintFile):
            os.remove(constraintFile)

    def ContextMenu(self, recipient):
        selection = [s  for s in FreeCADGui.Selection.getSelection() if s.Document == FreeCAD.ActiveDocument ]
        if len(selection) == 1:
            obj = selection[0]
            if hasattr(obj,'Content'):
                if 'ConstraintInfo' in obj.Content or 'ConstraintNfo' in obj.Content:
                    redefineCmd = {
                        'plane':'redefinePlaneConstraint',
                        'angle_between_planes':'redefineAngleConstraint',
                        'axial': 'redefineAxialConstraint',
                        'circularEdge' : 'redefineCircularEdgeConstraint',
                        'sphericalSurface' : 'redefineSphericalSurfaceConstraint'
                        }[ obj.Type ]
                    self.appendContextMenu( "Assembly2", [
                            'assemly2_animate_constraint',
                            redefineCmd,
                            'selectConstraintObjects',
                            'selectConstraintElements'])
            if 'sourceFile' in  obj.Content:
                self.appendContextMenu( 
                    "Assembly2", 
                    [ 'assembly2_movePart',
                      'assembly2_duplicatePart',
                      'assembly2_editImportedPart',
                      'assembly2_forkImportedPart',
                      'assembly2_deletePartsConstraints',
                      'assembly2_randomColorAll']
                    )

    Icon = ':/assembly2/icons/workBenchIcon.svg'

Gui.addWorkbench(Assembly2Workbench())


After modification of this two files, you have to start FreeCAD again. On Linux, it works for me with assembly2.
I see the new button in the toolbar of assembly2:
new-button.png
new-button.png (97.74 KiB) Viewed 2580 times
I select a constraint within my assembly...
constraint-selected.png
constraint-selected.png (276.98 KiB) Viewed 2580 times
and hit the new button:
highlighted-constraint.png
highlighted-constraint.png (594.99 KiB) Viewed 2580 times
Everything becomes transparently and you can have a look at the constraint. The two involved parts are hightlighted in the tree view...
Quit the dialog and everything looks like before.



Hope this helps (and urgently waiting for the next generation assembly....),

regards,
Klaus

P.S: Unfortunately you have to hit an extra button :|
KBWBE

https://github.com/kbwbe/A2plus
latest release: v0.4.56, installable via FreeCAD's addon manager
Tutorial: gripper assembly https://www.youtube.com/watch?v=QMxcQ5tssWk
Documentation: https://www.freecadweb.org/wiki/A2plus_Workbench
User avatar
easyw-fc
Veteran
Posts: 3633
Joined: Thu Jul 09, 2015 9:34 am

Re: Anyway to highlight the assembly constraints?

Post by easyw-fc »

Hi,
there is already a way to highlight A2 relations
https://forum.freecadweb.org/viewtopic. ... 40#p150135
a2-h.png
a2-h.png (66.92 KiB) Viewed 2572 times
kbwbe
Veteran
Posts: 1052
Joined: Tue Apr 10, 2018 3:12 pm
Location: Germany, near Köln (Cologne)

Re: Anyway to highlight the assembly constraints?

Post by kbwbe »

Sorry,

because I am still working with my on fork, i did not recognize this. Thanks for advice :D

regards,
Klaus
KBWBE

https://github.com/kbwbe/A2plus
latest release: v0.4.56, installable via FreeCAD's addon manager
Tutorial: gripper assembly https://www.youtube.com/watch?v=QMxcQ5tssWk
Documentation: https://www.freecadweb.org/wiki/A2plus_Workbench
User avatar
easyw-fc
Veteran
Posts: 3633
Joined: Thu Jul 09, 2015 9:34 am

Re: Anyway to highlight the assembly constraints?

Post by easyw-fc »

Not to be sorry at all,
your code is useful and can be used in other user cases :D
Koemi
Posts: 150
Joined: Thu Dec 28, 2017 11:13 am
Location: The Netherlands

Re: Anyway to highlight the assembly constraints?

Post by Koemi »

Hi kbwbe,

It works. Which is great. But it could be a bit more user-friendly, so here are 2 top tips from Koemi for free (if you like it or not...) 8-)

Top tip 1
After clicking 'show constraint' the messagebox 'Close Connection ViewMode' appears. Exactly in the middle of the image, THE place where you want to see the constraints you are looking for and not a message box which you have to move away from the center of the screen. If I just hit the escape button everything is back in order, so I would say: get rid of the messagebox .

Top tip 2
After clicking 'show constraint' the image is locked in position. :!: It would be nice if you still have the possibility to move, rotate and zoom the image which makes it possible to see the constraints which you are looking for. I think it is the messagebox which freezes the screen?

No worries. I will definately use this extra button. These are just friendly tips. Maybe it is a small change in the code. I am not a py-programmer so I am not able to adapt it.
Koemi
Posts: 150
Joined: Thu Dec 28, 2017 11:13 am
Location: The Netherlands

Re: Anyway to highlight the assembly constraints?

Post by Koemi »

Hi kbwbe,

still working with the "Show constraint"-button. I am glad to have it, but I noticed a small bug, which is maybe easy to change in the code :?:

Sometimes Assembly2 fundles around with constraints which are present in the combo view, but actually don't exist. I have no idea why these things happen... Assembly2 is buggy (and for some reason getting slow as hell when assemblies grow :cry: ), but I am very glad this workbench exists of course :!:

Anyway, if I click on one of these constraints and after that I click your "Show constraint"-button, the assembly becomes transparent and stays that way. Unfortunaly there is no escape from that, so I need to un-transparent all parts in the assembly manually...

Could this be a small change maybe?
Post Reply