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

macro to calculate coordinates from 3d sketch

  • Thread starter Thread starter alxG88
  • Start date Start date
I've come up with an idea (which will probably be a cabbage, but I'll show you anyway): I noticed that sw has a command called "division entity" that can divide a generic curve into many "pezzetti". these "pezzetti" are bounded each by two points (like each segment): if I set up a function that would call the command "split entities" and let him understand how many entities to divide my curve (and therefore, in how many points) I would solve my problem.
Do you think that's feasible?

hello and good holidays
 
In the meantime, I ask you an opinion on another solution that came to my mind: Starting from the macro you created, after it has identified the various sections of the curve, it is possible to "unite" the various entities so as to create a single one and divide it into a number of reference points through the instruction "swfeatmgr.insertreferencepoint"?

would be a great solution for me and I will explain why: for the work I have to do would be much more comfortable to split the entire curve into a number of points, and not the individual strokes, since I would like to do exactly points distributed throughout the curve and not only relative to the single stroke (in fact if a stretch is 10 mm long and another is 100 mm long the points will not be at the same distance between them along the two strokes, and this is a big problem).


Thank you.
 
from what I understand, you need to split a spline\poliline into equal parts, do not get the points of a profile to pass it to a cam program.
At this point we throw away everything and start again, and for this reason I wrote you a new macro exploiting refpoints.

prerequisites are:
- open side file with a sketch, the sketch should not be modified
- the sketch must be called "schizzo2", this only for the moment, because then the possible evolution of the macro will allow us to do what we want and select one of the many sketches present in our part.
- the sketch2 must contain a spline (not more than one I recommend, otherwise you have to enter control of the error)

the result will be:
- creation of user-defined reference points
- extrapolation in excel of points
- cleaning the feature manager, deleting the reference points.

the evolution of the macro at this point can:
- scan the sketch and check if there are more than one open profile
- if the profile is only one, but it is not a spline, it turns the profile into spline, erasing the entities that the year generates.
- extrapolate the required points
- physically create them in another sketch
- etc.

Good job
 

Attachments

In fact, what I need is a middle way between the two things you described. I need a macro that, given a definite curve at times (a stretch of spline, a straight one, a circumference, etc...) divides all the profile into a number of ref points. at this point the coordinates of these ref points should pass into a txt file.
the txt file will not end in a cam program, but it will simply be sent to the milling microcontroller who will think to convert those numbers into a language understandable by the engines.

so the macro you just wrote me is fine (except I would rather save in txt, but this is not a problem), but it only works for a spline, instead I would like it to work on a defined curve at times. So in my opinion it would take a fusion of the two macros you have written to me these days, so as to:
- scan all the entities in the sketch and recognize them (rect, spline, arc, ...)
- divide the entire curve into a number of ref points
- write the coordinates in txt
- cancel points

You say you can do something like that?


Hello and thank you
 
to make you understand better I attach an image of a curve I would like to get the coordinates. consists of 3 sections: two straight strokes and a stretch of spline.
what I would like is to divide the entire curve (not the individual strokes) into a number of ref points and calculate the coordinates of these points. once calculated copy them to a txt file. finally cancel the ref points.
Immagine.webp
 
I found a command that would do just in my case: "fit spline" (in Italian "fine spline"). practically this command, if you have a defined curve at times, unites all entities creating a single one in the form of a spline.
the problem that I can't find in the bees the command to call the education fit spline. If you could run this command from inside the macro, at this point the macro you wrote me a few days ago would be great (it would only be saved in txt and not in xls, but I think it is not a problem).
Do you know which code to call the "fit spline" command?

Thank you.
 
Come back. . .
the main problem is to create a polyline from the entities of your sketch, but it is feasible.
I melted the two macros I wrote to you, where at the end of scanning entities, I create a spline with tolerance +-0.001, which I then go to re-check after closing the sketch.
the result is I believe what you need... points for now are written in excel, but passing them to the limit in txt is not a problem, we have already done it in the first drafts of the macro.

requirements:
- part file with open sketch, happy one profile.

result:
- extrapolation of the resulting spline points, according to the user's request.

at the end of macro, if you enter your sketch and delete the spline, find under your original construction curves.

Let me know if we're here.

Hi.
 

Attachments

First of all, thank you very much!
I just have some curiosity to ask you:

- Does the macro also work with 3d sketches? because I tried but it gives me some mistakes
- so basically before I start the macro, I always have to create a unique spline through the "stimble spline" command? or can I simply draw my entities and then run the macro without having to optimize the spline?


Hello and thank you
 
for the second question I wrote to you (the one concerning the optimization of the spline) I have already answered myself: I had not noticed that it is the macro itself to create the spline starting from the various entities!: )
I only have the doubt of the operation or not with 3d sketches. if it also works with those then we are there, just make sure you save in txt and the game is done!

Hi.
 
in fact did not work in 3d sketches, everything depended on the string

set sketch = mypart.getactivesketch

whose call was obsolete, now I changed it

set sketch = mypart.getactivesketch2

and it turns well also with 3d sketches, it is enough now to export to txt and sie in place.

export to txt is the case that you write it, only because the writing order, spaces etc., must be set according to your needs, to the limit if you have problems let me know.

hello and good year to all
 

Attachments

perfect!!! at this point I will write the export in txt taking as point what you had written me in a previous macro and then I will post it on this discussion! !
in the meantime I thank you again and I wish you and all the users of the forum a good 2014! !

Hi.
 
Let me ask you one last piece of advice, as we are now at the end of work: I tried to write the macro piece to save in .txt but, although not having problems in the compilation phase, does not write correctly on the text file the coordinates.
the result is that it creates me ref points, creates me the text file and then deletes me ref points (so in theory all perfect). the problem is that the text file is composed of only "zero" values: In practice I have a txt file full of zeros (those columns of zeros how many ref points are).
I put the code I wrote, maybe I made a banal mistake of which I don't notice since I'm not practical of the vba.
Code:
    dim swapp                                  as sldworks.sldworks
    dim swmodel                                as sldworks.modeldoc2
    dim swfeat                                  as sldworks.feature
    dim swselmgr                               as sldworks.selectionmgr
    dim swsubfeat                             as sldworks.feature
    dim swsketch                               as sldworks.sketch
    dim swskpt                                  as sketchpoint
    dim sfeattype                              as string
    dim swrefpt                                 as sldworks.refpoint
    dim swrefptdata                           as sldworks.refpointfeaturedata
    dim swrefpointfeatdata                 as sldworks.refpointfeaturedata
    dim swmathpt                                as variant
    dim vswrefpt                                as sldworks.refpoint
    dim swmgr                                    as sketchmanager
    dim vfeat                                     as variant
    dim v                                           as variant
    dim i                                            as integer
    dim j                                            as long
    dim sfilename as string
    dim tfilename as string
    dim f as object
    dim fs as object
    dim bret                                    as boolean
    dim boolstatus as boolean
    dim longstatus as long, longwarnings as long
    dim nlength                         as double
    dim xvalue() as double
    dim yvalue() as double
    dim zvalue() as double
    dim point_count as integer

...
...
...


'scansione del feature manager, se trovo punti di riferimento li scrivo nel file .txt
    set swfeat = swmodel.firstfeature
    i = 1
    do while not swfeat is nothing
        sfeattype = swfeat.gettypename
         
            select case sfeattype
                case "refpoint"
                    if swfeat.name like "*punto*" then
                    set swrefpt = swfeat.getspecificfeature2
                    set swmathpt = swrefpt.getrefpoint
                        point_count = ubound(swmathpt)
                                redim xvalue(point_count)
                                redim yvalue(point_count)
                                redim zvalue(point_count)

                        for i = 0 to point_count

                    set vswrefpt = swmathpt(i)
        
                                xvalue(i) = swmathpt.arraydata(0) * 1000#
                                yvalue(i) = swmathpt.arraydata(1) * 1000#
                                zvalue(i) = swmathpt.arraydata(2) * 1000#
                           
                        next i

                        '
                        'creo il file .txt e ci scrivo sopra i valori
    
                        tfilename = swmodel.getpathname
                        sfilename = left(tfilename, len(tfilename) - 7)
                        set fs = createobject("scripting.filesystemobject")
                        set f = fs.createtextfile(sfilename & ".txt", true)
                         for i = 0 to point_count
                                f.writeline format(xvalue(i)) & " " & _
                                format(yvalue(i)) & " " & _
                                format(zvalue(i))
                        next i
                    
             end if
             
            end select
        set swfeat = swfeat.getnextfeature
      loop
   '
   'cancello i punti
   call deletepoints


ciao
 
in the row writing stage in the txt file, you have to add

for i = 0 to point_count
f.writeline format(xvalue(i) & " & _
format(yvalue(s)) & " & _
format(zvalue(s)) & vbcrlf
next i

so doing (vbcrlf) go to head
Let me know.
Hi :redface:
 
so doing it only changed the display of the zeros inside the txt file. now each row of zeros is separated from the next one by an empty row, but unfortunately zeros remain.
it seems in a few words that he can understand how many ref points calculate the coordinates, but cannot find them (for example, if I decide to have 5 ref points, at the end in the txt file I have five lines, each of which consists of three zeros).
 
I think I did! ! It was confusing because I had inserted a for cycle inside a loop and so the compiler did not understand what I was asking. I changed the code like this:

Code:
dim swapp                                  as sldworks.sldworks
    dim swmodel                                as sldworks.modeldoc2
    dim swfeat                                  as sldworks.feature
    dim swselmgr                        as sldworks.selectionmgr
    dim swsubfeat                              as sldworks.feature
    dim swsketch                        as sldworks.sketch
    dim swskpt                      as sketchpoint
    dim sfeattype                              as string
    dim swrefpt                                as sldworks.refpoint
    dim swrefptdata                 as sldworks.refpointfeaturedata
    dim swrefpointfeatdata                      as sldworks.refpointfeaturedata
    dim swmathpt                                as sldworks.mathpoint
    dim vswrefpt                    as sldworks.refpoint
    dim swmgr                       as sketchmanager
    dim vfeat                       as variant
    dim v                                      as variant
    dim i                                      as integer
    dim j                                      as long
    dim sfilename as string
    dim tfilename as string
    dim f as object
    dim fs as object
    dim bret                                    as boolean
    dim boolstatus as boolean
    dim longstatus as long, longwarnings as long
    dim nlength                         as double
    dim xvalue() as double
    dim yvalue() as double
    dim zvalue() as double


...
...
...


'scansione del feature manager, se trovo punti di riferimento li scrivo nel file .txt
    set swfeat = swmodel.firstfeature
    tfilename = swmodel.getpathname
    sfilename = left(tfilename, len(tfilename) - 7)
    set fs = createobject("scripting.filesystemobject")
    set f = fs.createtextfile(sfilename & ".txt", true)
    
    do while not swfeat is nothing
        sfeattype = swfeat.gettypename
         
            select case sfeattype
                case "refpoint"
                    if swfeat.name like "*punto*" then
                    set swrefpt = swfeat.getspecificfeature2
                    set swmathpt = swrefpt.getrefpoint
                        
                                redim xvalue(no_of_items)
                                redim yvalue(no_of_items)
                                redim zvalue(no_of_items)

                        

                    set vswrefpt = swmathpt(i)
        
                                xvalue(i) = swmathpt.arraydata(0) * 1000#
                                yvalue(i) = swmathpt.arraydata(1) * 1000#
                                zvalue(i) = swmathpt.arraydata(2) * 1000#
                           
                    
                                f.writeline format(xvalue(i)) & " " & _
                                format(yvalue(i)) & " " & _
                                format(zvalue(i)) & vbcrlf
                         
                                i = i + 1
      
             end if
                    
            end select
        set swfeat = swfeat.getnextfeature
      loop
   '
   'cancello i punti
   call deletepoints'scansione del feature manager, se trovo punti di riferimento li scrivo nel file .txt
    set swfeat = swmodel.firstfeature
    tfilename = swmodel.getpathname
    sfilename = left(tfilename, len(tfilename) - 7)
    set fs = createobject("scripting.filesystemobject")
    set f = fs.createtextfile(sfilename & ".txt", true)
    
    do while not swfeat is nothing
        sfeattype = swfeat.gettypename
         
            select case sfeattype
                case "refpoint"
                    if swfeat.name like "*punto*" then
                    set swrefpt = swfeat.getspecificfeature2
                    set swmathpt = swrefpt.getrefpoint
                        
                                redim xvalue(no_of_items)
                                redim yvalue(no_of_items)
                                redim zvalue(no_of_items)

                        

                    set vswrefpt = swmathpt(i)
        
                                xvalue(i) = swmathpt.arraydata(0) * 1000#
                                yvalue(i) = swmathpt.arraydata(1) * 1000#
                                zvalue(i) = swmathpt.arraydata(2) * 1000#
                           
                    
                                f.writeline format(xvalue(i)) & " " & _
                                format(yvalue(i)) & " " & _
                                format(zvalue(i)) & vbcrlf
                         
                                i = i + 1
      
             end if
                    
            end select
        set swfeat = swfeat.getnextfeature
      loop
   '
   'cancello i punti
   call deletepoints
is it right to you as a code setting or are there conceptual errors?

Hi.
 
I didn't try it because in the meantime I was creating a mophic parallel to the macro. .
try this attached, I entered a round in the coordinate value, which allows you to export only 3 decimal digits (just change it for having 4 or 5 or more).

Hi.
 

Attachments

Thank you very much, it works very well your version: is certainly more accurate than mine, which I wrote with my poor knowledge of the vba.

at this point I would say that the macro does exactly what I wanted to do: I renew my thanks for your precious help, without which everything would be enormously more difficult.

Before I conclude, I just wanted to understand every part of the code well, so as to enter properly in the perspective of writing a macro of this type, so maybe to be more prepared in the future if I had to write more or make changes to this.
In practice if I understand the macro well, it acts like this:

1- in the main is indicated to the macro to analyze, within a sketch, all the entities of the contour.

2- each boundary entity is scanned and identified through the "processsketchline, processsketchellipse, etc...". in practice the macro identifies each entity and understands whether it is a straight, an arc, an ellipse.. .

3- each boundary entity, once identified, is then passed to a function that extracts the data and at the end a spline is generated in tolerance +-0.001 mm

4- at this point is called "private sub points()", within which it settles in how many points to divide the spline, it performs the subdivision, it scans the feature manager and for each ref point found writes the relative coordinates on a txt file

5- Finally, it is called "deletepoints()" which provides for the elimination of the reference points generated.


Do I get it right here or do I get it wrong?


I only have a few doubts:

What is "getsubfeature()" which is called into question at the beginning of the main?

- I didn't understand how the function works. when it comes into action and what do you mean by "reloading data of every boundary entity"?


these should be the only doubts about the code (which, however, is fine, these are just my curiosity because I want to learn well to program on sw).


thanks again and hello
 
you understand all the steps from 1 to 5

As for "getsubfeature()" I need at the beginning macro to memorize the last fature of the tree, in fact I perform the scan starting from the bottom of the feature manager, and except the last feature in a variable.
When we move to the "deletepoints()", I begin to delete them from the bottom and question the name of the feature, I stop and go out when I find the feature stored at the beginning.
This is a system that I use to make the macro fast, in fact try to think of having a part with 200 features or more, starting to scan from above, I have to pass almost all the features to cancel at the end the extra ones, while so I leave from the bottom and stop where I need it.
It's some kind of undo.

with regard to the functions, if active from vba inside the drop-down menu displays-> immediate window, to each debug.print, read the result in this window (coordinates, type entities etc.), which opens below the headset.

try and let me know
Hi.
 

Forum statistics

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

Members online

No members online now.
Back
Top