• This forum is the machine-generated translation of www.cad3d.it/forum1 - the Italian design community. Several terms are not translated correctly.

macro error for import points from txt

omar91

Guest
Hello everyone, my name is omar and I am new to the forum. I am a mechanical engineering student (graduate) and I just started using solidworks.
not having found a command to import a set of points, I searched on the internet a macro that allowed me to do so. I found one that seemed to work and that I bring back:

sub main()

set swapp = application.sldworks
set part = swapp.activedoc
swapp.activedoc.activeview.framestate = 1
dim skpoint as object

open "c:\users\omar\desktop\material thesis\voluta\linee\prova.txt" for input as #1
part.sketchmanager.insert3dsketch true
do while not eof(1)
input #1, x, y, z
set skpoint = part.sketchmanager.createpoint(x, y, z)
loop


end

I say it seemed because the values of the points are distorted, e.g. 45,888 becomes 45888, 451,888444 becomes 451888.444, i.e. all numbers are increased of a factor 1000; moreover for values of the terna "small" (say below the tenth, although in reality it seems quite random), the numbers are read directly as 0 (for "resolvere" I thought to give in input reduced values of a factor 1000 x
for my abilities all this appears as "mysterious", can someone give me explanations? Thank you.
 
Hi.
the macro works well and as you said you have to divide the coordinates by 1000, this because the "call" to the "createpoint" bees, requires the values in meters, this for all that concerns programming in solidworks.
I suggest you always declare the right variables for the type of data to be contained, in your double case (double moving comma, is a type of given number with minimum two decimals, e.g. turns 35 in 35.00).
The createdpoint bees requires you to pass three double coordinates, vba not always, but for you this "translation".

I always recommend to the decimal separator, in your case the "." which must coincide with the one set in the additional settings in the country and language in the windows control panel, otherwise the computer interprets the numbers differently.

I allowed myself to change the macro by declaring differently the variable "swapp" and declaring x,y,z as double.
Moreover I have inserted not to see the graphic insertion of the entities in the creation phase inside the sketch, this speed exponentially the macro in the execution phase, especially when the points become many.
I closed the sketch.

p.s.:as you may know, if in front of everything you write you put a ' (apostrophe), the line is said to be commented and appears in green and is not read in the execution phase.

Good day
jenuary

here is the updated code

dim x as double
dim y as double
dim z as double
dim swapp as sldworks.sldworks
dim part as sldworks.modeldoc2
sub main()

set swapp = application.sldworks
set part = swapp.activedoc
swapp.activedoc.activeview.framestate = 1

dim skpoint as sldworks.sketchpoint

open "c:\prova.txt" for input as #1
part.sketchmanager.insert3dsketch true
'to freeze the graphics during insertion, everything is much faster
part.sketchmanager.addtodb = true

do while not eof(1)
input #1, x, y, z
set skpoint = part.sketchmanager.createpoint(x / 1000, y / 1000, z / 1000)
loop

'Reprise graphic insertion
part.sketchmanager.addtodb = false
'I close the sketch
part.sketchmanager.insert3dsketch false
end
 
Thank you, jenuary! probably when I tried to divide by 1000 into the macro I had wrong something and then returned 0.
I also thank you for the improvements you have made and that I will now try. in my specific case (let's say the most urgent one, for the thesis) the number of points is limited (some txt from about 50 points), but by curiosity I had tried also with a txt containing a much higher number of points and indeed after a while sw was crashing.
 

Forum statistics

Threads
44,997
Messages
339,767
Members
4
Latest member
ibt

Members online

No members online now.
Back
Top