I am attempting to execute a code using Rhino Python and I am having some issues with the following TypeError:
Message: not all arguments converted during string formatting
The code I have written is meant to read point coordinates from a file "newpoints.csv" and use them as arguments for Rhino Python's 'AddLine' function.
#!/usr/bin/env python
import rhinoscriptsyntax as rs
file = open("C:\\Users\\Seshane Mahlo\\Documents\\MSc Thesis\\newpoints.csv", "r")
lines = file.readlines()
file.close()
ab = len(lines)
seq = range(0, ab-1, 2)
coordinates = []
startvals = []
stopvals = []
for line in lines:
coords = line.split(',')
xcoord = float(coords[0])
ycoord = float(coords[1])
point = (xcoord, ycoord)
coordinates.append(point)
starts = range(0, ab-2, 2)
ends = range(1, ab+1, 2)
for i,j in zip(starts, ends):
strt = coordinates[i]
stp = coordinates[j]
rs.AddLine(start=strt,end=stp)
I think there is a small mistake in your code here:
starts = range(0, ab-2, 2)
ends = range(1, ab-1, 2)
which should be
starts = range(0, ab-1, 2)
ends = range(1, ab, 2)
because the last element you get from the range function is one less than the stop argument.
But what is causing the error is that you are trying to add a line, which is composed of two 3d points using a 2-tuple (x,y)
To fix this change:
point = (xcoord, ycoord)
to
point = (xcoord, ycoord, 0)
or whatever you want your z-coordinate to be.
Related
I would like learn if there is a decent and tensorflownic way to do follow conversion. Basically each string(row) as several words, and each word has a suffix like "%1", and goal is to strip string and only leave words with suffix value <= certain target value.
It is not hard to achieve using regular python programming. But I am thinking of adding the step to a tf computational graph, so a more tensorflownic way is preferred.
#From
text = tf.constant(['a1%0,a2%0,a3%1,a4%2,a5%3,a6%4','a7%3,a8%4',...]) #in shape of (n,) and n is large
#if target = 1, result will be
res = tf.ragged.constant([["a1", "a2", "a3"], [],...])
#if target = 3, result will be
res = tf.ragged.constant([["a1", "a2", "a3", "a4", "a5"], ["a7"],...])
You can do the following (tested in tensorflow 2.9)
text = tf.constant(['a1%0,a2%0,a3%1,a4%2,a5%3,a6%4','a7%3,a8%4',...])
target = 1
a = tf.strings.split(tf.strings.regex_replace(text, "%\d", ""), ",")
b = tf.strings.split(tf.strings.regex_replace(text, "[^,]*%", ""), ",")
b = tf.strings.to_number(b)
c = tf.ragged.boolean_mask(a, (b<=target))
I am trying to plot a polyline between multiple points in vba into autocad. I have almost finished my code, but the thing is that the points may repeat themselves, as 2 lines can have the same starting point, and the points are nor in a sorted way.
I need to be able to addd all points even if they aren't sorted, cause I have to keep the order of the points I am tryint to plot.
I am getting this error:
Invalid Procedure or argument call
Set acadPol = acadDoc.ModelSpace.AddLightWeightPolyline(Points)
This is my code:
Points(1)=9736.242889: Points(2)=9954.553808
Points(3)=9718.429708: Points(4)=9936.874562
If acadDoc.ActiveSpace = acModelSpace Then
Set acadPol = acadDoc.ModelSpace.AddLightWeightPolyline(Points)
Else
Set acadPol = acadDoc.PaperSpace.AddLightWeightPolyline(Points)
End If
acadPol.Closed = False
acadPol.Update
End If
End If
Your code is incomplete but I notice you have started your coordinates list at index 1.
There are plenty of examples on the internet
Sub Example_AddLightWeightPolyline()
' This example creates a lightweight polyline in model space.
Dim plineObj As AcadLWPolyline
Dim points(0 To 9) As Double
' Define the 2D polyline points
points(0) = 1: points(1) = 1
points(2) = 1: points(3) = 2
points(4) = 2: points(5) = 2
points(6) = 3: points(7) = 2
points(8) = 4: points(9) = 4
' Create a lightweight Polyline object in model space
Set plineObj = ThisDrawing.ModelSpace.AddLightWeightPolyline(points)
ZoomAll
End Sub
As you can see, you need to start your coordinates array at index 0 and not 1.
Does this help?
I’m trying to display data from csv file as a combination of contour and heat maps. CSV file consists of three columns:
x;y;z
1;1;1
2;1;1
3;1;2
4;1;2
5;1;2
6;1;2
7;1;2
8;1;1
9;1;1
10;1;1
1;2;1
2;2;1
3;2;2
4;2;4
5;2;4
Etc…
After reading csv I convert it to arrays and use them as data source for OxyPlot HeatMapSeries and ContourSeries according t examples I was able to find. Unfortunately, they are only in C#. Hope, someone can help with VB. So far, I created below listing, however result does not represent the data I have in csv.
Dim dt_table As DataTable = csvToDatatable_2(Application.StartupPath & "\test.csv", ";")
Dim x(dt_table.Rows.Count - 1) As Double
Dim y(dt_table.Rows.Count - 1) As Double
Dim numbers(dt_table.Rows.Count - 1, 2) As Double
For i = 0 To dt_table.Rows.Count - 1
x(i) = CDbl(dt_table.Rows(i)(0))
y(i) = CDbl(dt_table.Rows(i)(1))
numbers(i, 0) = CDbl(dt_table.Rows(i)(0))
numbers(i, 1) = CDbl(dt_table.Rows(i)(1))
numbers(i, 2) = CDbl(dt_table.Rows(i)(2))
Next
Dim Plotmodel As New OxyPlot.PlotModel
Dim linearColorAxis As New OxyPlot.Axes.LinearColorAxis()
linearColorAxis.Palette = OxyPalettes.Rainbow(10)
Plotmodel.Axes.Add(linearColorAxis)
Dim heatmapseries1 As New OxyPlot.Series.HeatMapSeries
heatmapseries1.X0 = 0
heatmapseries1.X1 = 10
heatmapseries1.Y0 = 0
heatmapseries1.Y1 = 10
heatmapseries1.Interpolate = True
heatmapseries1.Data = numbers
Dim contourSeries1 As New OxyPlot.Series.ContourSeries()
contourSeries1.LineStyle = LineStyle.Solid
contourSeries1.StrokeThickness = 2
contourSeries1.ContourColors = {OxyColors.Aquamarine, OxyColors.Aqua, OxyColors.CadetBlue, OxyColors.Blue, OxyColors.Black, OxyColors.Red}
contourSeries1.RowCoordinates = x
contourSeries1.ColumnCoordinates = y
contourSeries1.Data = numbers
Plotmodel.Series.Add(heatmapseries1)
Plotmodel.Series.Add(contourSeries1)
PlotView1.Model = Plotmodel
Even shape of Contourseries is not square, as expected:
I have a folder with several different files in it (txt, dat, jpg) and I need to read all the files with the end "triang.dat". These files contain their time on the filename as in:
"NIK_054504_triang.dat"
I managed to find the files and convert the times into seconds:
mypath = '/home/rmesqui/Desktop/Upleg/New/'
k=0
for file in os.listdir(mypath):
if file.endswith("triang.dat"):
k = k+1
filenames = np.zeros(k)
print filenames
k = 0
for file in os.listdir(mypath):
if file.endswith("triang.dat"):
#filenames[k] = file
filenames[k] =
float(file[4:6])*3600.+float(file[6:8])*60.+float(file[8:10])
k = k+1
timearr = np.sort(filenames)-np.min(filenames)
But I have to sort filenames because the procedure to read the filenames, returns out of order files. However, I need to read these files in order, since the time of the data taking is important for the rest of the program. As in, I need to have an array such as:
lat1 = np.zeros(shape=(100+3,numberOfFiles))
where the "+3" is the time, for our example, hour = 05, minutes = 45, seconds = 04. The "100" would be the contents of a particular column in the file.
Thanks y'all!
I found a simple way of doing that
for file in os.listdir(mypath):
if file.endswith("triang.dat"):
k = k+1
filenames = np.zeros(k)
k = 0
for file in os.listdir(mypath):
if file.endswith("triang.dat"):
#filenames[k] = file
filenames[k] = float(file[4:6])*3600.+float(file[6:8])*60.+float(file[8:10])
k = k+1
Still not fully sure about where exactly the problem lies. So what about this:
result = []
for filename in os.listdir(mypath):
if filename.endswith("triang.dat"):
hours, minutes, seconds = int(filename[4:6]), int(filename[6:8]), int(filename[8:10])
with open(filename, 'r') as f:
# do whatever needed to read the content from the file
your_100_values_read_from_the_file = range(100)
result.append([hours, minutes, seconds] + your_100_values_read_from_the_file)
# result is now a list of lists. sort by timestamp
result.sort(key=lambda x: (x[0], x[1], x[2]))
# create your array and transpose since you want one file per column, not per row
lat1 = np.array(result).T
print (lat1.shape) # should be (103, numberOfFiles)
First of all, I have been using this site as a reference through the entire scripting process and it has been wonderful. I appreciate how useful and knowledgeable everyone is here. With that in mind, I have a question regarding matching (pattern matching) in Lua. I am writing a script that essentially takes input from a file and imports it into a table. I am checking for specific MAC addresses in the file as the host I am querying.
if macFile then
local file = io.open(macFile)
if file then
for line in file:lines() do
local f = line
i, j = string.find ( f, "%x+" )
m = string.sub(f, i, j)
table.insert( macTable, m )
end
file:close()
end
This parses the file into a format I will use to query later. Once the table is built, I run a pattern matching sequence to try and match the MAC from the table by iterating the table and matching the pattern against the current iteration:
local output = {}
t = "00:00:00:00:00:00"
s = string.gsub( t, ":", "")
for key,value in next,macTable,nil do
a, p = string.find ( s, value )
matchFound = string.sub(s, a, p)
table.insert( output, matchFound )
end
This doesn't return any output although when I enter it line by line in a Lua prompt, it seems to work. The variables are being passed correctly I believe. Any suggestions?
If your macFile uses a structure like this:
012345678900
008967452301
000000000000
ffffffffffff
The following script should work:
macFile = "./macFile.txt"
macTable = {}
if macFile then
local hFile = io.open(macFile, "r")
if hFile then
for line in hFile:lines() do
local _,_, sMac = line:find("^(%x+)")
if sMac then
print("Mac address matched: "..sMac)
table.insert(macTable, sMac)
end
end
hFile:close()
end
end
local output = {}
t = "00:00:00:00:00:00"
s = string.gsub( t, ":", "")
for k,v in ipairs(macTable) do
if s == v then
print("Matched macTable address: "..v)
table.insert(output, v)
end
end
I am just leaving from work and can't have a deeper look at your problem right now, but the next two lines seem quite odd.
t = "00:00:00:00:00:00"
s = string.gsub( t, ":", "")
Basically you are removing all the ':' characters in the string t. Afterwards you end up with s being "000000000000". This is probably not what you want?