FreeCAD Logo FreeCAD 1.0
  • Anglais Afrikaans Arabe Biélorusse Catalan Tchèque Allemand Grec Espagnol Espagnol Basque Finnois Philippin Français Galicien Croate Hongrois Indonésien Italien Japonais Kabyle Coréen Lituanien Néerlandais Norvégien classique Polonais Portugais Portugais Roumain Russe Slovaque Slovène Serbe Suédois Turc Ukrainien Valencien Vietnamien Chinois Chinois
  • Fonctions
  • Télécharger
  • Blog
  • Documentation
    Index de la documentation Premiers pas Documentation pour les utilisateurs Manuel de FreeCAD Documentation des ateliers Documentation sur le codage en Python Documentation pour les développeurs Tutoriels Foire aux questions Politique de confidentialité À propos de FreeCAD
  • Contribuer
    Comment aider Sponsor Signaler un bogue Faire une demande de modification (PR) Emplois et financements Guide pour les contributions Manuel pour les développeurs Traductions
  • Communauté
    Code de conduite Forum The FPA GitHub GitLab Codeberg Mastodon Matrix IRC IRC via Webchat Gitter Discord Reddit Twitter Facebook LinkedIn Calendrier
  • ♥ Donate

Donate

$
Informations SEPA
Veuillez configurer votre virement bancaire SEPA pour:
Beneficiary: The FreeCAD project association
IBAN: BE04 0019 2896 4531
BIC/SWIFT: GEBABEBBXXX
Agence bancaire: BNP Paribas Fortis
Adresse: Rue de la Station 64, 1360 Perwez, Belgium

While Stripe doesn't support monthly donations, you can still become a sponsor! Simply make a one-time donation equivalent to 12 months of support, and you'll gain access to the corresponding sponsoring tier. It's an easy and flexible way to contribute.

If you are not sure or not able to commit to a regular donation, but still want to help the project, you can do a one-time donation, of any amount.

Choose freely the amount you wish to donate one time only.

You can support FreeCAD by sponsoring it as an individual or organization through various platforms. Sponsorship provides a steady income for developers, allowing the FPA to plan ahead and enabling greater investment in FreeCAD. To encourage sponsorship, we offer different tiers, and unless you choose to remain anonymous, your name or company logo will be featured on our website accordingly.

from 1 USD / 1 EUR per month. You will not have your name displayed here, but you will have helped the project a lot anyway. Together, normal sponsors maintain the project on its feet as much as the bigger sponsors.

from 25 USD / 25 EUR per month. Your name or company name is displayed on this page.

from 100 USD / 100 EUR per month. Your name or company name is displayed on this page, with a link to your website, and a one-line description text.

from 200 USD / 200 EUR per month. Your name or company name and logo displayed on this page, with a link to your website and a custom description text. Companies that have helped FreeCAD early on also appear under Gold sponsors.

Instead of donating each month, you might find it more comfortable to make a one-time donation that, when divided by twelve, would give you right to enter a sponsoring tier. Don't hesitate to do so!

Choose freely the amount you wish to donate each month.

Please inform your forum name or twitter handle as a notein your transfer, or reach to us, so we can give you proper credits!

Generic macro icon BSurf_from_grid

Description
Crée une surface B-spline à travers une grille de points.

Version macro : 0.1
Date dernière modification : 2022-07-31
Auteur: heda
Auteur
heda
Téléchargement
None
Liens
Page des macros
Comment installer une macro
Comment créer une barre d'outils
Version Macro
0.1
Dernière modification
2022-07-31
Version(s) FreeCAD
None
Raccourci clavier
None
Voir aussi
None

Description

Crée une surface B-spline à travers une grille de points. Options permettant de créer uniquement des courbes polylignes ou B-splines.

Macro BSurf from grid

Installation

Disponible dans le Gestionnaire des extensions.

Options

Voir le docstring.

Utilisation

Voir le docstring.

Script

Macro_BSurf_from_grid.FCMacro


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

# ***************************************************************************
# *                                                                         *
# *   Copyright (c) 2022 - heda <heda@fc-forum>                             *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
# *   as published by the Free Software Foundation; either version 2 of     *
# *   the License, or (at your option) any later version.                   *
# *   for detail see the LICENCE text file.                                 *
# *                                                                         *
# *   This program is distributed in the hope that it will be useful,       *
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
# *   GNU Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with this program; if not, write to the Free Software   *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# ***************************************************************************

__Name__ = 'BSurf_from_grid'
__Comment__ = 'Makes a BSpline surface through a grid of points.'
__Author__ = 'heda'
__Version__ = '0.1'
__Date__ = '2022-07-31'
__License__ = 'LGPL-2.0-or-later'
__Web__ = ''
__Wiki__ = 'https://wiki.freecad.org/Macro_Grid_2_BSurf'
__Icon__ = ''
__Help__ = 'Select structured point-cloud, set options, run macro'
__Status__ = 'functional'
__Requires__ = ''
__Communication__ = ''
__Files__ = ''

"""
inspired by:
https://forum.freecad.org/viewtopic.php?f=22&t=69941&start=10
cred to snow54

it is assumed that the cloud is:
    - structured
    - aligned with xy grid,
i.e. a constant y divides 2 horizontal rows of points,
analogue for x and vertical columns of points


usage:
select a pointcloud and run macro    

options:
wires: convert boundary wires to spline (draft wb),
       in surf wb, use boundry edges and verts on wires as vertex-constraints

splines: in surf wb, use outer contour as boundry edges,
         inner edges can be used as edge constraints

bsurf: surface directly created with poles/knots from interpolated spline

mkwframe: in case of bsurf, symbolic wireframe of splines (draft wb)
"""

import numpy as np
from scipy import interpolate

import FreeCAD as App
import FreeCADGui as Gui
import Draft, Part

Vector = App.Vector

doc = App.ActiveDocument

try:
    cloud, = Gui.Selection.getSelection()
except ValueError as e:
    raise RuntimeWarning('a point cloud needs to be selected.')

points = cloud.Points.Points
bb = cloud.Points.BoundBox

options = o = dict(MakeWires=False,
                   MakeBSplines=False,
                   MakeBSurf=True,
                   MakeWFrame=True)

## a bit of options logic...
if o['MakeWires']: o['MakeBSplines'] = False
if o['MakeBSplines']: o['MakeBSurf'] = o['MakeWires'] = False
if o['MakeBSurf'] and o['MakeWFrame']:
    o['MakeBSplines'] = True; o['MakeWires'] = False
if not o['MakeBSurf']: o['MakeWFrame'] = False


def RGB(*args):
    return tuple((float(i/255) for i in args))


hstep = min((p.y for p in points))
vstep = min((p.x for p in points))

wiregroup = doc.addObject('App::DocumentObjectGroup', 'Wires')
hwires = doc.addObject('App::DocumentObjectGroup', 'HWires')
vwires = doc.addObject('App::DocumentObjectGroup', 'VWires')
wiregroup.addObjects([hwires, vwires])

for wg, step, getter in ((hwires, hstep, 'y'),
                         (vwires, vstep, 'x')):
    wires = list()
    empty = False
    cc = bb.YMin if getter == 'y' else bb.XMin
    cc -= step/2
    while not empty:
        polypoints = list()
        for pt in points:
            value = getattr(pt, getter)
            if cc < value < cc + step:
                polypoints.append(pt)
        if polypoints:
            if o['MakeWires'] or (o['MakeBSurf'] and not o['MakeWFrame']):
                maker = Draft.make_wire
            elif o['MakeBSplines']:
                maker = Draft.make_bspline
            wires.append(maker(polypoints, closed=False))
            cc += step
        else:
            empty = True

    wg.addObjects(wires)

Gui.Selection.clearSelection()

if o['MakeBSurf']:
    degree_u = degree_v = 3
    periodic = False

    lowerx, lowery = hwires.Group[0], vwires.Group[0]
    x = np.array([pt.x for pt in lowerx.Points])
    y = np.array([pt.y for pt in lowery.Points])
    Z = np.array([[pt.z for pt in hw.Points] for hw in hwires.Group])

    ## Create spline surface
    g = interpolate.RectBivariateSpline(x, y, Z.T)
    coefgs = g.get_coeffs()
    coefgs = np.reshape(coefgs, Z.T.shape).T
    knots = g.get_knots()
    # Convert knot vector format
    knot_u, mult_u = np.unique(knots[1], return_counts=True)
    knot_v, mult_v = np.unique(knots[0], return_counts=True)
    # Normalize knot vectors
    knot_u = (knot_u - knot_u.min()) / (knot_u.max() - knot_u.min())
    knot_v = (knot_v - knot_v.min()) / (knot_v.max() - knot_v.min())
    
    # Calculate spline coefficients for x and y
    gx = interpolate.make_interp_spline(x, x)
    coefgx = gx.tck[1]
    gy = interpolate.make_interp_spline(y, y)
    coefgy = gy.tck[1]

    getVector = lambda iz, jz: Vector(coefgx[jz],
                                      coefgy[iz],
                                      coefgs[iz, jz])

    # Create control point vectors
    ctrl = [[getVector(iZ, jZ) for jZ, lc in enumerate(lr)]
            for iZ, lr in enumerate(coefgs)]
    
    # Create spline surface in FreeCAD
    bs = Part.BSplineSurface()
    bs.buildFromPolesMultsKnots(ctrl, mult_u, mult_v, knot_u, knot_v,
                                periodic, periodic, degree_u, degree_v)
    
    surf = Part.show(bs.toShape(), 'BSplineSurf')
    surf.Label2 = 'spline surface through points'
    surf.ViewObject.ShapeColor = RGB(0, 170, 255)

    if not o['MakeWFrame']:
        for obj in hwires.Group + vwires.Group + [hwires, vwires, wiregroup]:
            doc.removeObject(obj.Name)


doc.recompute()

av = Gui.ActiveDocument.ActiveView
av.viewIsometric()
av.fitAll()

Cette page est extraite de https://wiki.freecad.org/Macro_BSurf_from_grid

Contactez-nous !
Forum GitHub Mastodon Matrix IRC Gitter.im Discord Reddit Twitter Facebook LinkedIn

© L'équipe FreeCAD. Crédits des images de la page d'accueil (de haut en bas) : ppemawm, r-frank, epileftric, regis, rider_mortagnais, bejant.

Ce projet est soutenu par : , KiCad Services Corp. et autres parrains

GitHubAméliorer cette page sur GitHub