[Solved] CSV help

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
jfc4120
Posts: 448
Joined: Sat Jul 02, 2022 11:16 pm

[Solved] CSV help

Post by jfc4120 »

Code: Select all

OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.20.2.29177 +426 (Git)
Build type: Release
Branch: (HEAD detached from 0.20.2)
Hash: 930dd9a76203a3260b1e6256c70c1c3cad8c5cb8
Python 3.8.10, Qt 5.15.2, Coin 4.0.1, Vtk 8.2.0, OCC 7.6.3
Locale: English/United States (en_US)
Installed mods: 
  * Help 1.0.3
This little test file works:

Code: Select all

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

import FreeCAD,FreeCADGui
import Draft
import math
import csv
from PySide import QtGui

WIRE = True
INPUTININCHES = True
if INPUTININCHES:
    convert = 25.4
else:
    convert =1.0

getDouble = QtGui.QInputDialog.getDouble
Vector, Placement = App.Vector, App.Placement
doc = App.ActiveDocument

FIELDS = ["Number_sides", "Factor"]
with open('c:\\mydocs\\factor.csv', "r") as f:
    mycsv = csv.DictReader(f)
    for row in mycsv:
        if row["Number_sides"] == "16":
            print(row["Factor"])
            break
It gives expected value in the print.

However this one gives nothing, no response, any idea why not?

Code: Select all

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

import FreeCAD,FreeCADGui
import Draft
import math
import csv
from PySide import QtGui

WIRE = True
INPUTININCHES = True
if INPUTININCHES:
    convert = 25.4
else:
    convert =1.0

getDouble = QtGui.QInputDialog.getDouble
Vector, Placement = App.Vector, App.Placement
doc = App.ActiveDocument

sel = Gui.Selection.getSelectionEx()
start_point = Vector(0, 0, 0)
if sel:
    sel, = sel
    if sel.PickedPoints:
        start_point = sel.PickedPoints[-1]

FLAG=0

TCLRD, tclrdflag = getDouble(None, 'example', 'Radius', decimals=3)
#TCLRD *= convert

BASEON, baseonflag = getDouble(None, 'example', 'POLYGON number of sides 12, 16, 24, 36, 48', decimals=0)

FIELDS = ["Number_sides", "Factor"]
with open('c:\\mydocs\\factor.csv', "r") as f:
    mycsv = csv.DictReader(f)
    for row in mycsv:
        based = str(BASEON)
        if row["Number_sides"] == based:  # a hard coded number works
            print(row["Factor"])
            break
Edit:

Got it I had to:

Code: Select all

based = str(int(BASEON))
It was reading like 12.0 instead of 12. Sorry I found an S.O. post after posting.
Post Reply