Problème avec Macro Shake_Sketch

Forum destiné aux questions et discussions en français
Forum rules
Be nice to others! Read the FreeCAD code of conduct!
mario52
Veteran
Posts: 4692
Joined: Wed May 16, 2012 2:13 pm

Re: Problème avec Macro Shake_Sketch

Post by mario52 »

Bonjour

je parlais du fichier de Henri33 qui fait arrêter FreeCAD

pour le sketch à quatre lignes elle ne fait rien , sauf si je déplace un point

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.
galou_breizh
Posts: 436
Joined: Wed Sep 15, 2010 9:38 am

Re: Problème avec Macro Shake_Sketch

Post by galou_breizh »

Je suis l'auteur de cette macro. J'ai déjà fait des correction pour améliorer le support de FreeCAD v0.17 avec l'introduction des Part.LineSegment dans les sketchs. Cependant, la macro ne marche toujours pas et je vais poster en anglais pour demander de l'aide.

Gaël
User avatar
Henri33
Posts: 59
Joined: Sat Dec 23, 2017 3:43 pm

Re: Problème avec Macro Shake_Sketch

Post by Henri33 »

galou_breizh wrote: Tue Jan 23, 2018 8:31 am Je suis l'auteur de cette macro. J'ai déjà fait des correction pour améliorer le support de FreeCAD v0.17 avec l'introduction des Part.LineSegment dans les sketchs. Cependant, la macro ne marche toujours pas et je vais poster en anglais pour demander de l'aide.

Gaël
.
Je suis certain qu'elle finira par fonctionner. L'important, c'est d'avoir consacré une partie de ton temps à vouloir aider les autres.
Et ça c'est formidable.
galou_breizh
Posts: 436
Joined: Wed Sep 15, 2010 9:38 am

Re: Problème avec Macro Shake_Sketch

Post by galou_breizh »

Grâce à abdullah, la macro Shake_Sketch devrait fonctionner à nouveau !

Gaël
madtiger
Posts: 1
Joined: Tue Jun 19, 2018 11:16 pm

Re: Problème avec Macro Shake_Sketch

Post by madtiger »

Bonsoir,
voici la macro qui fonctionne pour la V0.17

Bonne utilisation!!
Madtiger

Code: Select all

# -*- coding: utf-8 -*-

# FreeCAD macro to shake a sketch in order to discover its unconstrained parts.
#
# A Gaussian noise is introduced in all sketch points and the sketch is then
# solved.
# Beware that the sketch can look different because some constraints have
# several solutions. In this case, just undo.
#
# This file is released under the MIT License.
# Author: Gaël Ecorchard
# Version:
# - 1.1, 2014-10-31
#     * correct import for Part
# - 1.0, 2014-08, first release.

# Amplitude of the point displacements.
# The standard deviation of the Gaussian noise is the largest sketch dimension
# multiplied by this factor.
displacement_amplitude = 0.1

# End of configuration.

from random import gauss

import FreeCADGui as Gui
from FreeCAD import Base
import Part

# For each sketch geometry type, map a list of points to move.
g_geom_points = {
    Base.Vector: [1],
    Part.LineSegment: [1, 2],  # first point, last point
    Part.Circle: [0, 3],  # curve, center
    Part.ArcOfCircle: [1, 2, 3],  # first point, last point, center
}


class BoundingBox(object):
    xmin = None
    xmax = None
    ymin = None
    ymax = None

    def enlarge_x(self, x):
        if self.xmin is None:
            self.xmin = x
            self.xmax = x
            return
        if self.xmin > x:
            self.xmin = x
            return
        if self.xmax < x:
            self.xmax = x
            return

    def enlarge_y(self, y):
        if self.ymin is None:
            self.ymin = y
            self.ymax = y
            return
        if self.ymin > y:
            self.ymin = y
            return
        if self.ymax < y:
            self.ymax = y
            return

    def enlarge_point(self, point):
        self.enlarge_x(point.x)
        self.enlarge_y(point.y)

    def enlarge_line(self, line):
        self.enlarge_x(line.StartPoint.x)
        self.enlarge_x(line.EndPoint.x)
        self.enlarge_y(line.StartPoint.y)
        self.enlarge_y(line.EndPoint.y)

    def enlarge_circle(self, circle):
        self.enlarge_x(circle.Center.x - circle.Radius)
        self.enlarge_x(circle.Center.x + circle.Radius)
        self.enlarge_y(circle.Center.y - circle.Radius)
        self.enlarge_y(circle.Center.y + circle.Radius)

    def enlarge_arc_of_circle(self, arc):
        # TODO: correctly compute the arc extrema (cf. toShape().BoundBox)
        self.enlarge_x(arc.Center.x)
        self.enlarge_y(arc.Center.y)


def get_sketch_dims(sketch):
    bbox = BoundingBox()
    for geom in sketch.Geometry:
        if isinstance(geom, Base.Vector):
            bbox.enlarge_point(geom)
        elif isinstance(geom, Part.LineSegment):
            bbox.enlarge_line(geom)
        elif isinstance(geom, Part.Circle):
            bbox.enlarge_circle(geom)
        elif isinstance(geom, Part.ArcOfCircle):
            bbox.enlarge_arc_of_circle(geom)
    if (bbox.xmin is not None) and (bbox.ymin is not None):
        return bbox.xmax - bbox.xmin, bbox.ymax - bbox.ymin
    else:
        return 0, 0


def add_noise(point, sigma):
    """Add a Gaussian noise with standard deviation sigma"""
    point.x = gauss(point.x, sigma)
    point.y = gauss(point.y, sigma)


def move_points(sketch, geom_index, sigma):
    point_indexes = g_geom_points[type(sketch.Geometry[geom_index])]
    # Direct access to sketch.Geometry[index] does not work. This would,
    # however prevent repeated recompute.
    for point_index in point_indexes:
        point = sketch.getPoint(geom_index, point_index)
        add_noise(point, sigma)
        sketch.movePoint(geom_index, point_index, point)

view_provider = Gui.activeDocument().getInEdit()

# Don't know how to exit from a macro.
do_move = True
if not view_provider:
    do_move = False

if do_move:
    sketch = view_provider.Object

    if sketch.TypeId != 'Sketcher::SketchObject':
        do_move = False

if do_move:
    sigma = max(get_sketch_dims(sketch)) * displacement_amplitude
    print(sketch.Geometry)
    for i in range(len((sketch.Geometry))):
        move_points(sketch, i, sigma) 

chrisb
Veteran
Posts: 54197
Joined: Tue Mar 17, 2015 9:14 am

Re: Problème avec Macro Shake_Sketch

Post by chrisb »

Pour la version 0.18 actuelle, ce n'est plus necessaire d'utiliser Shake_Sketch. Abdullah a ajouter une nouvelle fonction pour trouver les degré de liberte:
Bildschirmfoto 2018-06-20 um 09.08.14.png
Bildschirmfoto 2018-06-20 um 09.08.14.png (7.45 KiB) Viewed 602 times
clicquez le lien montre les element qui ne sont pas suffisament contraint.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
Post Reply