[solved]Draft Non-parametric array

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

[solved]Draft Non-parametric array

Post by xianyu »

I know I may repeat the topic.But, I have searched for several days and still haven't found a solution.
There is very little information about the non parametric array. (For my code, the Parametric array is OK, but this is not OK)
My question is this:
When I use "App:: PropertyLinkList" to link three objects. When I array, I only get the parent object, but not the three child objects.
https://forum.freecadweb.org/viewtopic.php?f=3&t=74100

Code: Select all

obj.addProperty('App::PropertyLinkList', 'Links', 'Lens', 'FreeCAD objects to be lenses').Links = [front_Surface, rear_Surface, edge_Surface]
xianyu wrote: Fri Dec 02, 2022 8:19 am array(objectslist, xvector, yvector, zvector, xnum, ynum, znum).
I tried a variety of "objectslist". This seems a bit difficult to solve.
I'm sorry I can't provide ". FCStd" because it doesn't work


edit: I made a similar example, just change the "Draft. array (obj,)" in "def NonParametric_array (self, obj):"
Copy the code to the python console, and select "NonParametric" from the array's Power

Code: Select all

import FreeCAD as App
import Part
import Draft
import FreeCAD, FreeCADGui, Part
def create():
    """
    Object creation method
    """
    obj = App.ActiveDocument.addObject('Part::FeaturePython', "name")
    objCylinder = Part.show(Part.makeCylinder(5,10))
    box(obj, objCylinder)
    ViewProviderBox(obj.ViewObject)
    App.ActiveDocument.recompute()
    return obj

class box():
    def __init__(self, obj, objCylinder):
        """
        Default constructor
        """
        self.Type = 'box'
        self.objCylinder = objCylinder
        obj.addProperty("App::PropertyEnumeration","Power","Array","Lens Array").Power=["Off",  "NonParametric"]
        obj.addProperty('App::PropertyLength', 'Length', 'Dimensions', 'Box length').Length = 10
        obj.addProperty('App::PropertyLength', 'Width', 'Dimensions', 'Box width').Width = 10
        obj.addProperty('App::PropertyLength', 'Height', 'Dimensions', 'Box height').Height = 10
        obj.addProperty('App::PropertyLinkList', 'Links', 'Lens', 'FreeCAD objects to be lenses').Links = [objCylinder]
        obj.Proxy = self

    def execute(self, obj):
        """
        Called on document recompute
        """
        obj.Shape = Part.makeBox(obj.Length, obj.Width, obj.Height)

    def onChanged(self, obj, prop):
        if prop == "Power":
            if obj.Power == "NonParametric":
                self.NonParametric_array(obj)

    def NonParametric_array(self, obj):
        objlist = [obj, self.objCylinder]
        Draft.array(obj,FreeCAD.Vector(20, 0, 0), 
                        FreeCAD.Vector(0, 20, 0), 
                        FreeCAD.Vector(0, 0, 0),
                        3,3,1)

class ViewProviderBox(object):

    def __init__(self, vobj):
        """
        Set this object to the proxy object of the actual view provider
        """
        self.Object = vobj.Object
        vobj.Proxy = self

    def attach(self, vobj):
        """
        Setup the scene sub-graph of the view provider, this method is mandatory
        """
        self.Object = vobj.Object
        self.onChanged(vobj, '')

    def updateData(self, fp, prop):
        """
        If a property of the handled feature has changed we have the chance to handle this here
        """
        pass

    def getDisplayModes(self,obj):
        """
        Return a list of display modes.
        """
        return []

    def getDefaultDisplayMode(self):
        """
        Return the name of the default display mode. It must be defined in getDisplayModes.
        """
        return "Shaded"

    def setDisplayMode(self,mode):
        """
        Map the display mode defined in attach with those defined in getDisplayModes.
        Since they have the same names nothing needs to be done.
        This method is optional.
        """
        return mode

    def onChanged(self, vp, prop):
        """
        Print the name of the property that has changed
        """
        pass

    def onDelete(self, feature, subelements):
        '''Here we can do something when the feature will be deleted'''
        return True

    def claimChildren(self):
        '''Return a list of objects that will be modified by this feature'''
        return self.Object.Links
        
    def getIcon(self):
        """
        Return the icon in XMP format which will appear in the tree view. This method is optional and if not defined a default icon is shown.
        """
        return """
            /* XPM */
            static const char * ViewProviderBox_xpm[] = {
            "16 16 6 1",
            "    c None",
            ".   c #141010",
            "+   c #615BD2",
            "@   c #C39D55",
            "#   c #000000",
            "$   c #57C355",
            "        ........",
            "   ......++..+..",
            "   .@@@@.++..++.",
            "   .@@@@.++..++.",
            "   .@@  .++++++.",
            "  ..@@  .++..++.",
            "###@@@@ .++..++.",
            "##$.@@$#.++++++.",
            "#$#$.$$$........",
            "#$$#######      ",
            "#$$#$$$$$#      ",
            "#$$#$$$$$#      ",
            "#$$#$$$$$#      ",
            " #$#$$$$$#      ",
            "  ##$$$$$#      ",
            "   #######      "};
            """

    def __getstate__(self):
        """
        Called during document saving.
        """
        return None

    def __setstate__(self,state):
        """
        Called during document restore.
        """
        return None


create()
Last edited by xianyu on Fri Dec 09, 2022 2:49 am, edited 1 time in total.
Freecad novice, A Python enthusiast
chrisb
Veteran
Posts: 53930
Joined: Tue Mar 17, 2015 9:14 am

Re: Draft Non-parametric array

Post by chrisb »

This looks rather like a Python topic. Should I move it?
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

Re: Draft Non-parametric array

Post by xianyu »

chrisb wrote: Tue Dec 06, 2022 7:11 am This looks rather like a Python topic. Should I move it?
No problem, this is really a script problem.Thank
Freecad novice, A Python enthusiast
chrisb
Veteran
Posts: 53930
Joined: Tue Mar 17, 2015 9:14 am

Re: Draft Non-parametric array

Post by chrisb »

Moved.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: Draft Non-parametric array

Post by Roy_043 »

In NonParametric_array you create an objlist that you then do not use.
IMO it does not make sense to create a non-parametric array in this context.
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

Re: Draft Non-parametric array

Post by xianyu »

Roy_043 wrote: Tue Dec 06, 2022 9:14 am In NonParametric_array you create an objlist that you then do not use.
IMO it does not make sense to create a non-parametric array in this context.
Yes, that list is only for my testing.
For me, after the array, each Lens can be modified independently. Only non parameter arrays can achieve this function (parameter arrays are all modified uniformly).

Or there are other ways to copy the parent object and child object?
Freecad novice, A Python enthusiast
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

Re: Draft Non-parametric array

Post by xianyu »

I think I should have found the key to the problem
https://forum.freecadweb.org/viewtopic. ... 29#p426329
You may also consider use 'with_dependencies' as the default behavior. When App::Link is found in the dependencies, copyObject() with_dependency will not include the linked object, but it will duplicate the link itself. In case you do want to duplicate everything including the linked object, you can manually call App.getDependentObjects(obj) to obtain the full dependency list, and pass the returned list of objects to copyObject().
Is this the solution?
Freecad novice, A Python enthusiast
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: [solved]Draft Non-parametric array

Post by Roy_043 »

I do not know what you want to achieve. An array (in CAD) means a repetition of the same object. If you want individual elements you should not use an array.
Post Reply