FreeCAD Logo FreeCAD 1.0
  • angielski afrykanerski arabski białoruski kataloński czeski niemiecki grecki hiszpański hiszpański baskijski fiński filipiński francuski galicyjski chorwacki węgierski Indonezyjski włoski japoński kabylski koreański litewski duński Norweski Bokmal polski portugalski portugalski rumuński rosyjski słowacki słoweński serbski szwedzki turecki ukraiński walenciański wietnamski chiński chiński
  • Funkcjonalność programu
  • Pobierz
  • Blog
  • Dokumentacja
    Spis dokumentacji Jak zacząć Dokumentacja użytkowników Podręcznik do programu FreeCAD Dokumentacja środowisk pracy Dokumentacja skryptów środowiska Python Dokumentacja kodowania C++ Poradniki Najczęściej zadawane pytania Polityka prywatności O FreeCAD
  • Przyłącz się do projektu
    Jak pomóc Sponsor Zgłoś błąd Utwórz pull request Praca i finansowanie Zasady współpracy Podręcznik dla programistów Tłumaczenia
  • Społeczność
    Kodeks postępowania Forum The FPA GitHub GitLab Codeberg Mastodon Matrix IRC IRC via Webchat Gitter Discord Reddit Twitter Facebook LinkedIn Kalendarz
  • ♥ Donate

Donate

$
Informacje o SEPA
Skonfiguruj przelew bankowy SEPA do:
Beneficiary: The FreeCAD project association
IBAN: BE04 0019 2896 4531
BIC/SWIFT: GEBABEBBXXX
Bank: BNP Paribas Fortis
Adres: 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. Create your personal icon with the same name of the macro BSurf_from_grid

Description
Makes a B-spline surface through a grid of points.

Macro version: 0.1
Last modified: 2022-07-31
Author: heda
Author
heda
Download
None
Links
Macros recipes
How to install macros
How to customize toolbars
Macro Version
0.1
Date last modified
2022-07-31
FreeCAD Version(s)
None
Default shortcut
None
See also
None

Description

Makes a B-spline surface through a grid of points. Options for only creating Wire or B-spline curves.

Macro BSurf from grid

Installation

Available in the Addon manager.

Options

See docstring.

Usage

See 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()

Ta strona pochodzi z https://wiki.freecad.org/Macro_BSurf_from_grid

Bądźmy w kontakcie!
Forum GitHub Mastodon Matrix IRC Gitter.im Discord Reddit Twitter Facebook LinkedIn

© Załoga FreeCAD. Autorami grafiki na stronie głównej (od góry do dołu) są: ppemawm, r-frank, epileftric, regis, rider_mortagnais, bejant.

Ten projekt jest wspierany przez: , KiCad Services Corp. oraz pozostałych sponsorów

GitHubUlepsz tę stronę na GitHub