Make points coincident when building a sketch.

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
browntoastjam
Posts: 37
Joined: Fri Aug 20, 2021 9:51 pm

Make points coincident when building a sketch.

Post by browntoastjam »

Hello All,

I am building a sketch from vertices extracted from kicad. The problem is some of the vertices differ by a very small value.
For example one vertex is 37.2834 and the other is 37.28. I want these points to be coincident.

Is there some way to automatically make points coincident (after the sketch is build) if points differ by a small delta.

Here is the code that build my sketch:

Code: Select all

doc = freecad.app.newDocument()
sketch = doc.addObject("Sketcher::SketchObject", "EdgeCuts")

for x in edges:

    if x.GetShape() == 0:  # 0 is line segment
        temp_x0=x.GetStart0()
        temp_x1=x.GetEnd0()
        
        sketch.addGeometry(Part.LineSegment(App.Vector(temp_x0.x/SCALE, temp_x0.y/SCALE, 0.0),
                                            App.Vector(temp_x1.x/SCALE, temp_x1.y/SCALE, 0.0)), False)

    if x.GetShape() == 2: #Arc
        x0=x.GetStart()
        m=x.GetArcMid()    
        x1=x.GetEnd()
        arc = Part.Arc(App.Vector(x0.x/SCALE, x0.y/SCALE, 0.0), App.Vector(m.x/SCALE, m.y/SCALE, 0.0), App.Vector(x1.x/SCALE, x1.y/SCALE, 0.0))
        sketch.addGeometry(arc,False)
The resultant sketch is not closed because of the non-coincident, but yet too close, points.
I could keep a vertex list, and compare the euclidean distance when adding a new point.

Code: Select all

if (distance > delta) add to new vertex to list.
But is there a easier way to 'clean' up vertices?

Image
ec.FCStd
Example of sketched created from scripting
(4.73 KiB) Downloaded 8 times
Thanks.
edwilliams16
Veteran
Posts: 3106
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Make points coincident when building a sketch.

Post by edwilliams16 »

It doesn't matter how close the points are numerically, sketcher won't connect them unless you add Coincidence (or Tangent) constraints.

Look at https://wiki.freecadweb.org/Sketcher_scripting to see how it is done. Or https://github.com/FreeCAD/FreeCAD/blob ... Example.py
chrisb
Veteran
Posts: 53919
Joined: Tue Mar 17, 2015 9:14 am

Re: Make points coincident when building a sketch.

Post by chrisb »

browntoastjam wrote: Mon Jun 27, 2022 6:00 pm But is there a easier way to 'clean' up vertices?
Sketcher ValidateSketch can make such points coincident with a selectable tolerance.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
browntoastjam
Posts: 37
Joined: Fri Aug 20, 2021 9:51 pm

Re: Make points coincident when building a sketch.

Post by browntoastjam »

chrisb wrote: Mon Jun 27, 2022 9:42 pm
browntoastjam wrote: Mon Jun 27, 2022 6:00 pm But is there a easier way to 'clean' up vertices?
Sketcher ValidateSketch can make such points coincident with a selectable tolerance.
Can you run validateSketch from a script?
chrisb
Veteran
Posts: 53919
Joined: Tue Mar 17, 2015 9:14 am

Re: Make points coincident when building a sketch.

Post by chrisb »

browntoastjam wrote: Tue Jun 28, 2022 12:59 am Can you run validateSketch from a script?
I wouldn't bet on it. It needs an expert to answer this.
Abdullah wrote:ping
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
abdullah
Veteran
Posts: 4935
Joined: Sun May 04, 2014 3:16 pm
Contact:

Re: Make points coincident when building a sketch.

Post by abdullah »

chrisb wrote: Tue Jun 28, 2022 10:06 pm
browntoastjam wrote: Tue Jun 28, 2022 12:59 am Can you run validateSketch from a script?
I wouldn't bet on it. It needs an expert to answer this.
Abdullah wrote:ping
Autoconstraining routines should work fine here.

In your simple sketch, this works:

Code: Select all

ActiveSketch.autoconstraint(0.1)
0.1 is the tolerance for distances. After this command it correctly pads.

This feature is explained here:
https://forum.freecadweb.org/viewtopic. ... 95#p260195

There are routines to only detect some constraints and apply them. There is room for improvement in the routines, as the solver has improved a lot since they were written, and now there are better ways to code the tool. In the meantime, this should work in most of the cases.

Btw, user @easyw-fc, is our KiCAD to FreeCAD specialist. He knows all the tricks existing, and part of those that do not yet exist :lol: .
abdullah
Veteran
Posts: 4935
Joined: Sun May 04, 2014 3:16 pm
Contact:

Re: Make points coincident when building a sketch.

Post by abdullah »

chrisb wrote: Tue Jun 28, 2022 10:06 pm Ping
BTW, yesterday I removed the obsolete Sketcher tools :)
browntoastjam
Posts: 37
Joined: Fri Aug 20, 2021 9:51 pm

Re: Make points coincident when building a sketch.

Post by browntoastjam »

abdullah wrote: Wed Jun 29, 2022 11:59 am

In your simple sketch, this works:

Code: Select all

ActiveSketch.autoconstraint(0.1)
Thank you so much.
FreeCAD scripting is really fun to use even for a beginner like me.
chrisb
Veteran
Posts: 53919
Joined: Tue Mar 17, 2015 9:14 am

Re: Make points coincident when building a sketch.

Post by chrisb »

abdullah wrote: Wed Jun 29, 2022 12:01 pm BTW, yesterday I removed the obsolete Sketcher tools :)
Top!
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: Make points coincident when building a sketch.

Post by Roy_043 »

abdullah wrote: Wed Jun 29, 2022 12:01 pm BTW, yesterday I removed the obsolete Sketcher tools :)
I gather you mean Sketcher_CloseShape and Sketcher_ConnectLines. I guess we need to keep those Wiki pages for now, and maybe add a note. Which tools are the successors of these tools, if any?
Post Reply