How do I transform a point cloud to a line?

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
more11
Posts: 124
Joined: Sun Sep 28, 2014 11:03 am
Contact:

How do I transform a point cloud to a line?

Post by more11 »

I am pretty new to FreeCAD. I have used it a couple of years ago, so I don't remember much.

I have successfully imported a points cloud to FreeCAD. I want do define a line from this cloud. I don't know which type of line to use, but at this stage it doesn't matter much. I will use the line to define a surface.

The points are twodimensional, z is always 0.



Do I need to do this in Python?`

Thanks

Mårten
Point taken :-)
User avatar
quick61
Veteran
Posts: 3803
Joined: Sat Aug 24, 2013 2:49 am
Location: u.S.A.

Re: How do I transform a point cloud to a line?

Post by quick61 »

more11 wrote:I am pretty new to FreeCAD. I have used it a couple of years ago, so I don't remember much.

I have successfully imported a points cloud to FreeCAD. I want do define a line from this cloud. I don't know which type of line to use, but at this stage it doesn't matter much. I will use the line to define a surface.

The points are twodimensional, z is always 0.



Do I need to do this in Python?`

Thanks

Mårten
Hello Mårten, Welcome (back) to FreeCAD and the forum.

To automate the process, yes, you would use Python. I would use either Draft Wire or Draft BSpline.

I hope that your not using the same install of FreeCAD that yo were a couple of years ago. ;) You should at least be using the current release, 0.14.37xx.

A search through the Python scripting and macros section of the forum should turn up some examples. FreeCAD also has a .dat file importer that imports points used in describing airfoils and FEM objects.

I think you will find that FreeCAD is a bit different than it was a few years ago. There have been a couple of improvements during that time. :P

Also, please read - IMPORTANT: Please read first before asking for help. Providing your FreeCAD information as described in that post is always helpful.

Mark
This post made with 0.0% Micro$oft products - GOT LINUX?
mario52
Veteran
Posts: 4692
Joined: Wed May 16, 2012 2:13 pm

Re: How do I transform a point cloud to a line?

Post by mario52 »

hi more11 welcome
can you give an example of your cloud fcstd file ?
which file you pull your cloud ?
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.
more11
Posts: 124
Joined: Sun Sep 28, 2014 11:03 am
Contact:

Re: How do I transform a point cloud to a line?

Post by more11 »

Thanks for replies.

I use the latest FreeCAD for windows.

My cloud file: https://dl.dropboxusercontent.com/u/106 ... -radie.asc

I made a DWIRE manually, it was only about 75 points. I had to redo it twice. It works quite well with snapping.

FreeCAD is still a bit buggy. But since it is free one cannot complain :-). I save often and go back when unexpected things happen. I have had a lot of problems with rotate. The auto working plane doesnt work as expected, you need to shift it when you shift view. Importing dxf was problematic. The correct macro directory was hidden in a thread, and of course I downloaded the html files instead of the zip. Finding the report tab was helping as well.

I am pretty happy with what I have acomplished today, despite a few bugs.
Image

Mårten
Point taken :-)
jmaustpc
Veteran
Posts: 11207
Joined: Tue Jul 26, 2011 6:28 am
Location: Australia

Re: How do I transform a point cloud to a line?

Post by jmaustpc »

more11 wrote:I use the latest FreeCAD for windows.
Please post you Help about FreeCAD data as that comment does not define which one of the "latest" versions you are using. I assume you mean you downloaded one of the precompiled 0.14 release versions from Sourceforge, but its annoying having to guess.

If you want to do more advance things with point clouds, you may want to compile FreeCAD with PCL support (Point Cloud Library), I don't know if it is in those windows builds. I compiled it once a long time ago and from memory you could then create a solid from the points cloud ...but I can not now remember the details.
more11 wrote:FreeCAD is still a bit buggy.
If you find something wrong post your Help about FreeCAD data, a file that demonstrates the bug and exact step by step instructions to repeat the bug, ....if the bug is a bug and is still there in the development version, it will likely be fixed...in the development version..
mario52
Veteran
Posts: 4692
Joined: Wed May 16, 2012 2:13 pm

Re: How do I transform a point cloud to a line?

Post by mario52 »

hi
This a small macro create wire (or points) of your file (coordinate x y z)

Code: Select all

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
# created a wire with coordinate x y z separated (in the file without coma)
__title__= "Macro_WireXYZ"
__author__= "Mario52"
__date__= "2020/10/16"
__version__= "00.03"
##
#EX:
#0 0 0
#10 10 10
#15 20 25
#. . . .

from FreeCAD import Base
import Draft, Part

## path for Windows    : C:\yourPath\cloud.asc (create one error in Python (cause, the "\" is a command syntax in Python)
## replace "\" by "/"  result : C:/yourPath/cloud.asc 
## or replace the "\" by "\\" result : C:\\yourPath\\cloud.asc 

fichier = "C:\\yourPath\\cloud.asc"                          # path and name of file.txt

file = open(fichier, "r")                                  # open the file read
wire = []
X=Y=Z = 0.0

for ligne in file:
    coordinates = ligne.split()
    try:                                                        # for format PCD ignore the header
        X,Y,Z = coordinates                                     # separate the coordinates
#        Draft.makePoint(float(X),float(Y),float(Z))            # create points (uncomment for use)
        print(X," ",Y," ",Z)
        wire.append(FreeCAD.Vector(float(X),float(Y),float(Z))) # append the coordinates
    except Exception:
        None
file.close()
Draft.makeWire(wire,closed=False,face=False,support=None)   # create the wire open
#Draft.makeWire(wire,closed=True,face=False,support=None)   # create the wire closed (uncomment for use)

#Draft.makeBSpline(wire,closed=False,face=False,support=None)# create the BSpline open (uncomment for use)
#Draft.makeBSpline(wire,closed=True,face=False,support=None)# create the BSpline open (uncomment for use)

App.ActiveDocument.recompute()

for your problems you can ask for help on this forum there are a lot of passionate volunteers to help
but it is important as stated quick61 IMPORTANT: Please read first before asking for help. read and give your version information

EDIT: 16/10/2020 11h36 Paris : upgrade the macro for Python3 and the remark of Rowan

mario
Last edited by mario52 on Fri Oct 16, 2020 9:40 am, edited 1 time in total.
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.
more11
Posts: 124
Joined: Sun Sep 28, 2014 11:03 am
Contact:

Re: How do I transform a point cloud to a line?

Post by more11 »

I have tested the code and it works.

However, in order to reuse this functionality the input should be a point cloud. There is already GUI functionality to import a point cloud.
Point taken :-)
Rowan
Posts: 3
Joined: Tue Oct 13, 2020 4:06 am

Re: How do I transform a point cloud to a line?

Post by Rowan »

Hi,

I'm trying to do this same thing (take a file with columns for x,y and z coordinates separated by a space, and a new line for the next coordinate, and make it into a line in FreeCAD). I haven't generated the point cloud yet but was just trying using the "Stanford Bunny", which I found here: https://github.com/FREECAD/Examples/blo ... -Bunny.asc. I copied all of those points into Notepad and saved it as Bunny.asc.

I should mention I've never used Python and it has been a long time since I did any programming really. I copied the macro above, saved it as "CreateWire.FCMacro", and tried to run it. Obviously I changed the file path to point to where I had saved the bunny file.

Initially I was getting the error message:
<unknown exception traceback><class 'SyntaxError'>: ('invalid syntax', ('C:/Users/rm/AppData/Roaming/FreeCAD/Macro/CreateWire.FCMacro', 17, 11, ' print X," ",Y," ",Z\n'))

I tried just commenting out that print line (I'm not even sure where it was printing to - is there some sort of output or debugging window? Or was that the crucial part where it printed a point on my model?) with a # at the start, and instead got the error message:
<unknown exception traceback><class 'SyntaxError'>: ("(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \\UXXXXXXXX escape", ('C:/Users/rm/AppData/Roaming/FreeCAD/Macro/CreateWire.FCMacro', 7, 10, 'fichier = "C:\\Users\\rm\\Documents\\Rowan\\3DPrint\\Bunny.asc" # path and name of file.txt\n'))

I notice there seems to be a double backslash appearing in there, for some reason, but have no idea if that is relevant. Also notice that the Macro provided appears to have some french terms in it, I tried changing fichier to file and ligne to line just in case my installation of Python needed them to be english terms, but it didn't change anything. I also tried saving my point cloud as Bunny.txt and changing the target to Bunny.txt but again it didn't change anything.

Any suggestions would be greatly appreciated.
Thanks,
Rowan


OS: Windows 10
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.4 (GitTag)
Build type: Release
Branch: releases/FreeCAD-0-18
Hash: 980bf9060e28555fecd9e3462f68ca74007b70f8
Python version: 3.6.6
Qt version: 5.6.2
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/Australia (en_AU)
jmaustpc
Veteran
Posts: 11207
Joined: Tue Jul 26, 2011 6:28 am
Location: Australia

Re: How do I transform a point cloud to a line?

Post by jmaustpc »

Your first post so firstly...Welcome to FreeCAD! :)

I am a big hurry just now, but I am pretty sure the problem is just that you are trying to run an old Python2 macro in Python3. The syntax has changed in Ptython3 for some things including the "print" statement.

I have to go now...

Jim
User avatar
Roy_043
Veteran
Posts: 8547
Joined: Thu Dec 27, 2018 12:28 pm

Re: How do I transform a point cloud to a line?

Post by Roy_043 »

There are two issues with the code:
  1. FreeCAD has meanwile switched to Python 3. This means that print X," ",Y," ",Z should now be print(X," ",Y," ",Z).
  2. On the fichier = line you should single forward slashes or, if you OS is Windows, double backslashes as the path separator.
Note that you can directly import a point cloud in FreeCAD.
Post Reply