FreeCAD_PipeFromXYZ

# -*- coding: utf-8 -*-
# 10/26/2017 6:54:16 AM
# this works so keep a copy of it
# For Python, PEP 8 has emerged as the style guide
# http://docs.python-guide.org/en/latest/intro/learning/
# https://www.freecadweb.org/wiki/Code_snippets
# https://github.com/FreeCAD/FreeCAD
import FreeCAD,Draft,Arch
# In Python, all the statements indented by the same number of character spaces
# after a programming construct are considered to be part of a single block of code.
# Python uses indentation as its method of grouping statements.
# https://becksteinlab.physics.asu.edu/pages/courses/2013/SimBioNano/03/I…
## @package Makewire
# \ingroup ARCH
# \brief Creates DWire object from file of 3D coordinates
#
# This program opens a file, reads each space delimited line
# and draws a dwire object
__title__ = "DWire Import"
__author__ = "Greg Robinson"
__url__ = "http://Lucrosol.com"
# example that works
# p1 = FreeCAD.Vector(0,0,1)
# p2 = FreeCAD.Vector(1,1,2)
# p3 = FreeCAD.Vector(2,4,3)
# Draft.makeWire([p1,p2,p3],closed=False)
# https://www.freecadweb.org/wiki/Draft_API
# hard coded path & file of coordinates
# Example:
#105507.460938 91080.125 1331.37109375
#105509.648438 91077.9375 1326.85534668
#105501.375 91072.890625 1318.00634766
#105487.0 91070.3984375 1318.89746094
#105482.851562 91068.8203125 1318.02026367
#105480.5625 91063.5234375 1318.0456543
#105480.351562 90950.0859375 1318.84057617
#105475.992188 90940.046875 1319.13378906
#105473.546875 90933.515625 1318.09472656
#105473.820312 90897.3359375 1321.03942871
#105473.820312 90897.3359375 1321.03942871
#105475.671875 90889.4140625 1276.28381348
#105454.164062 90909.0078125 1274.7479248
#105453.164062 90917.8671875 1274.77026367
# Find & Replace Examples
# x = [s.replace('a', 'b') for s in x]
# words = [w.replace('[br]', '') for w in words]
#
# function readfile
def fileread():
myfile = open("C:/Users/Greg/Desktop/txt/CAT_806.txt")
# Create empty set
global lines
lines = []
for l in myfile.readlines():
lines.append(l)
myfile.close()
fileread()
# End readfile function

# contents of file are now in list
# Note
# Lists are enclosed in square brackets ([ and ]) and tuples in parentheses (( and )).
numbers = []
dwire_list = []
# Process list from file into float then get them into vectors
for line in lines:
newline = [float(x) for x in line.split()]
dwire_list.append(newline)
# append the processed items together

a_list = []
coords = []
count = len(dwire_list)
while (count > 0):
coord0 = FreeCAD.Vector(dwire_list[count - 1])
coords.append(coord0)
count = count - 1
#
print "Loop Executed, Dwire should have appeared on file"
# Creat Dwire
# Draft.makeWire(coords,closed=False)
# "makePipe([baseobj,diameter,length,placement,name]): creates an pipe object from the given base object"
# Create Dwire | line = Draft.makeWire(points,closed=False,face=True,support=None)
# Previous Way: Draft.makeWire(coords,closed=False)
print coords
line = Draft.makeWire(coords,closed=False,face=True,support=None)
# Example: App.ActiveDocument.addObject("Part::Cylinder","Cylinder")
# Example: App.ActiveDocument.ActiveObject.Label = "Cylinder"

#
# clwire = App.ActiveDocument.DWire ???
# FreeCAD.ActiveDocument.recompute()
#
Arch.makePipe(line, 2.00)
# regenerate
FreeCAD.ActiveDocument.recompute()

============

; ASCPOINT.LSP Copyright 1990-97 Tony Tanzillo All Rights Reserved.
; Revised June 16, 2013 Greg Robinson - Mode 6 line 313 chg'd
;; Author: Tony Tanzillo,
;; Design Automation Consulting
;; http://ourworld.compuserve.com/homepages/tonyt
;; tony.tanzillo@worldnet.att.net
;; Block extension by V.Michl, CAD Studio, 2012:
;; http://www.cadstudio.cz http://www.cadforum.cz
;;
;; Permission to use, copy, modify, and distribute this software
;; for any purpose and without fee is hereby granted, provided
;; that the above copyright notice appears in all copies and
;; that both that copyright notice and the limited warranty and
;; restricted rights notice below appear in all copies and all
;; supporting documentation, and that there is no charge or fee
;; charged in return for distribution or duplication.
;;
;; This SOFTWARE and documentation are provided with RESTRICTED
;; RIGHTS.
;;
;; Use, duplication, or disclosure by the Government is subject
;; to restrictions as set forth in subparagraph (c)(1)(ii) of
;; the Rights in Technical Data and Computer Software clause at
;; DFARS 252.227-7013 or subparagraphs (c)(1) and (2) of the
;; Commercial Computer Software Restricted Rights at 48 CFR
;; 52.227-19, as applicable. The manufacturer of this SOFTWARE
;; is Tony Tanzillo, Design Automation Consulting.
;;
;; NO WARRANTY
;;
;; ANY USE OF THIS SOFTWARE IS AT YOUR OWN RISK. THE SOFTWARE
;; IS PROVIDED FOR USE "AS IS" AND WITHOUT WARRANTY OF ANY KIND.
;; TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THE AUTHOR
;; DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING, BUT
;; NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND
;; FITNESS FOR A PARTICULAR PURPOSE, WITH REGARD TO THE SOFTWARE.
;;
;; NO LIABILITY FOR CONSEQUENTIAL DAMAGES. TO THE MAXIMUM
;; EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT SHALL
;; THE AUTHOR OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL,
;; INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER
;; (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF
;; BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
;; INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF
;; THE USE OF OR INABILITY TO USE THE SOFTWARE PRODUCT, EVEN
;; IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
;; DAMAGES. BECAUSE SOME JURISDICTIONS DO NOT ALLOW EXCLUSION
;; OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL
;; DAMAGES, THE ABOVE LIMITATION MAY NOT APPLY TO YOU.
;;
;
; ASCPOINT.LSP is a utility for use with AutoCAD Release 10 or later,
; which reads coordinate data from ASCII files in CDF or SDF format,
; and generates AutoCAD geometry from the imported coordinates.
;
; The ASCPOINT command will read coordinate data from an ASCII file,
; and generate either a continuous string of LINES, a POLYLINE, a
; 3DPOLYline, multiple copies of a selected group of objects, or
; AutoCAD POINT entities.
;
; Format:
;
; Command: ASCPOINT
; File to read: MYFILE.TXT <- ASCII input file
; Comma/Space delimited : Comma <- data format
; Generate Copies/Lines/Nodes/3Dpoly/: Nodes <- entity to create
; Reading coordinate data...
;
; If you selected "Copies", then ASCPOINT will prompt you to select the
; objects that are to be copied. The basepoint for all copies is the
; current UCS origin (0,0,0). One copy of the selected objects will be
; created for each incoming coordinate, using each coordinate as the
; displacement relative to the origin.
;
; A comma-delimited (CDF) ascii file contains one coordinate per line,
; with each component seperated by a comma, like this:
;
; 2.333,4.23,8.0
; -4.33,0.0,6.3
; 0.322,5.32,0.0,attribute1,attribute2
; etc....
;
; There should be no spaces or blank lines in a CDF coordinate data file.
;
; A space-delimited (SDF) ascii file contains one coordinate per line,
; with each component seperated by one or more spaces, like this:
;
; 2.333 4.23 8.0
; -4.33 0.0 6.3
; 0.322 5.32 0.0 attribute1 attribute2
; ...
;
; Coordinate data can be 2D or 3D.
;
; Note that all numeric values must have at least one digit to the left
; and the right of the decimal point (values less than one must have a
; leading 0), and a leading minus sign indicates negative values. This
; applys to both CDF and SDF formats.
;
; ASCPOINT can generate a continuous chain of LINE entities from your
; coordinate data, where each pair of adjacent lines share a coordinate
; from the file.
;
; ASCPOINT can also generate a polyline or 3DPOLYline from the coordinate
; data, where each point in the file becomes a vertice of the polyline.
; If the input file contains 3D coordinates, and you specify a polyline,
; then the Z component is ignored and the default of 0.0 is used.
;
; ASCPOINT will also COPY a selected group of objects, creating one copy
; for each incoming coordinate, and using the coordinate as the absolute
; copy displacement from the CURRENT UCS origin (0,0,0).
;
; Finally, ASCPOINT will generate AutoCAD POINT entities from the data in
; the file. Specify the point size and type prior to invoking ASCPOINT.
;
; Writing POINT coordinates to file:
;
; The WPOINT command also included in this file, will export the
; coordinates of selected POINT entities to a comma-delimited CSV
; file that can be read into Excel, and imported using ASCPOINT.
;
; Good luck,
;
; Tony Tanzillo

(defun C:ASCPOINT ( / f bm hi format input line plist ss makepoint blname pt i)
(cond ( (not (setq f (getfiled "Import ASCII Coordinate Data"
"" "" 0))))
( (not (setq f (open f "r")))
(princ "\nCan't open file for input."))
(t (initget "Space Comma")
(setq format
(cond ((getkword "\nComma/Space delimited : "))
(t "Comma")))
(initget "Copies Inserts Lines Nodes 3Dpoly Pline")
(setq input
(cdr (assoc
(cond
( (getkword
"\nGenerate Copies/Inserts/Lines/Nodes/3Dpoly/: "))
(t "Pline"))
'(("Lines" . "._LINE")
("Copies" . "._COPY")
("Inserts" . "._-INSERT")
("Nodes" . "._POINT")
("3Dpoly" . "._3DPOLY")
("Pline" . "._PLINE"))))
)
(setq read-point
(if (eq format "Comma") cdf sdf)
)
(setq makepoint
(if (eq input "._PLINE") 2dpoint (if (eq input "._-INSERT") anypoint 3Dpoint)) ; VM
)
(setvar "cmdecho" 0)
(command "._UNDO" "_Begin")
(setq bm (getvar "blipmode"))
(setq hi (getvar "highlight"))
(setvar "blipmode" 0)
(princ "\nReading coordinate data...")
(while (setq line (read-line f))
(cond
( (and (setq line (strtrim line))
(/= line "")
(setq line (makepoint (read-point line))))
(setq plist (cons line plist)))))
(close f)
(setq plist (reverse plist))
(cond ( (eq input "._POINT")
(setvar "highlight" 0)
(command "._POINT" "0,0,0"
"._COPY" (setq ss (entlast)) "" "_m" "0,0,0")
(apply 'command plist)
(command)
(entdel ss))

( (eq input "._COPY")
(princ "\nSelect objects to copy,")
(while (not (setq ss (ssget)))
(princ "\nNo objects selected,")
(princ " select objects to copy,"))
(setvar "HIGHLIGHT" 0)
(command "._COPY" ss "" "_m" "0,0,0")
(apply 'command plist)
(command))

( (eq input "._-INSERT")
(setq ss (entsel "\nSelect the block to insert (may have attributes): "))
(if ss (progn
(setq blname (cdr (assoc 2 (entget(car ss)))))
(princ blname)
(foreach pt plist
(command "._-INSERT" blname (3dpoint pt) 1 1 0)
(setq i 3)(while (> (getvar "CMDACTIVE") 0)(command (toSym (nth i pt)))(setq i (1+ i))) ; attributes
)
));if
)

(t (command input)
(apply 'command plist)
(command)))

(command "._UNDO" "_en")
(setvar "highlight" hi)
(setvar "blipmode" bm)))
(princ)
)

(defun cdf (l / s)
; (command "._LASTPOINT" l)
; (getvar "lastpoint")
; VM:
(setq s (vl-string-translate "," " " l)); maybe also ";"
(read (strcat "(" s ")"))
)

(defun sdf (l)
(read (strcat "(" l ")"))
)

(defun 3dpoint (p)
(list (car p) (cadr p) (cond ((caddr p)) (t 0.0)))
)

(defun 2dpoint (p)
(list (car p) (cadr p))
)

(defun anypoint (p)
p
)

(defun toSym (s) ; VM
(if (eq (type s) 'SYM)
(vl-symbol-name s)
(if s s "")
)
)

(defun noz (p)
(list (car p) (cadr p))
)

;; ================================================================
;; (Strtrim )
;;
;; Trims leading and trailing spaces from

(defun strtrim (s)
(Strltrim (Strrtrim s))
)

;; ================================================================
;; (StrLtrim )
;;
;; Trims leading spaces from

(defun Strltrim (s / l)
(if (wcmatch s " *")
(progn
(setq l (1+ (strlen s)) i 1)
(while
(and (eq (substr s i 1) " ")
(/= l i))
(setq i (1+ i))
)
(substr s i)
)
s
)
)

;; ================================================================
;; (StrRtrim )
;;
;; Trims trailing spaces from

(defun Strrtrim (s / i)
(if (wcmatch s "* ")
(progn
(setq i (strlen s))
(while
(and (> i 0)
(eq (substr s i 1) " "))
(setq i (1- i))
)
(substr s 1 i)
)
s
)
)

(defun C:WPOINT ( / ss fd file)
(cond
( (not (setq ss (ssget '((0 . "POINT"))))))
( (not (setq file (getfiled "Export Points" "" "txt" 1))))
( (not (setq fd (open file "w")))
(alert "Unable to open file for output"))
(t (repeat (setq i (sslength ss))
(write-point
(ssname ss (setq i (1- i)))
fd
6
)
)
(close fd)
)
)
(princ)
)

(defun write-point (e fd prec / p)
(setq p (cdr (assoc 10 (entget e))))
(write-line
(strcat (rtos (car p) 2 prec) ","
(rtos (cadr p) 2 prec) ","
(rtos (caddr p) 2 prec)
)
fd
)
)

(princ "\nASCPOINT.LSP Copyright 1990-1997 Tony Tanzillo.")
(princ "\nRevised Greg Robinson June 16, 2013.")
(princ "\nUse ASCPOINT to import coordinates.")
(princ "\nUse WPOINT to export POINT coordinates.")
(princ)