![]() |
【转帖】draw shaft using visual basic application
draw shaft using visual basic application
hello there.........i've developed a system that can draw shaft using vba.....there is no problem to draw shaft with 1 diameter......but when i want to draw shaft in three different diameter , problem occured...... private sub commandbutton5_click() 'shaft 1 set swapp = application.sldworks set part = swapp.activedoc set selmgr = part.selectionmanager swapp.activedoc.activeview.framestate = 1 dim skcircle as object set skcircle = part.sketchmanager.createcircle(0.4091243888205, -0.2921278626156, 0, txta.text, -0.3123621415489, 0) part.clearselection2 true boolstatus = part.extension.selectbyid2("arc1", "sketchsegment", 0, 0, 0, false, 0, nothing, 0) part.featuremanager.featureextrusion2 true, false, false, 0, 0, txtb.text, txtb.text, false, false, false, false, 0.01745329251994, 0.01745329251994, false, false, false, false, 1, 1, 1, 0, 0, false part.selectionmanager.enablecontourselection = 0 part.activateselectedfeature part.clearselection2 true 'shaft 2 set swapp = application.sldworks set part = swapp.activedoc set selmgr = part.selectionmanager swapp.activedoc.activeview.framestate = 1 set skcircle = part.sketchmanager.createcircle(0, 0, txtb.text, txtc.text, 0.0323604905812, 0) part.clearselection2 true boolstatus = part.extension.selectbyid2("arc1", "sketchsegment", 0, 0, 0, false, 0, nothing, 0) part.featuremanager.featureextrusion true, false, false, 0, 0, txtd.text, txtd.text, false, false, false, false, 0.01745329251994, 0.01745329251994, false, false, false, false, 1, 1, 1 part.selectionmanager.enablecontourselection = 0 part.clearselection2 true end sub this is my codding........ my problem is 2 cylinder is start in the same reference.....i want my program first draw 1 cylinder,then another cylinder is extrude from it surface not from reference...... this would most likely be easier to do just using configurations and the design library. considering screws have will have a finite number of different sizes to choose from. well from what i am seeing your setting skcricle as a circle object and putting in the properties for it. then you reset skcircle as another circle and put different properties in it referencing the same "arc1" sketch seg. try dim skcircle1 as object and then dim skcircle2 as object this way they are seperate objects and you can manipulate them individually. example: dim x as string x = "first string" then right after that you do x = "second string" if you msgbox x x will = second string because your overwritting it when you reassign it. _______________________________________ now if i would have said dim x as string dim y as string and said x = "first string" y = "second string" i could msgbox x to see the text in the x var or i could msgbox y to see the y var i also noticed you are setting a bunch of objects a second time. i don;t know that it creates an issue although it is redundant and creates overhead. you could remove: set swapp = application.sldworks set part = swapp.activedoc set selmgr = part.selectionmanager swapp.activedoc.activeview.framestate = 1 in the second cylinder because those objects are already established up above. hope this helps. solidworks professional 2009 sp0 dual core amd opteron 2.21ghz cpu 4 gb ram nvidia quadrofx 4500 w/ 512mb ram you would need to insert a user form and capture your values and then plug them into your existing code. i will write it up quick and post it here. solidworks professional 2009 sp0 dual core amd opteron 2.21ghz cpu 4 gb ram nvidia quadrofx 4500 w/ 512mb ram 1. insert a userform 2. change the name property in the userform to frmrevolve 3. add three labels, three text boxes and a command button. 4. change property the caption of each label to: diameter distance angle 5. change the name property of each textbox to txtdiameter txtdistance txtangle 6. change the name of the command button to bok 7. change the caption of the command button to ok 8. resize all of the controls to what you want by dragging them around. 9. double click on the ok button you created and add the following code. public diameter as double public distance as double public angle as double private sub bok_click() 'make sure the user is entering numbers and not text if isnumeric(txtdiameter.text) and isnumeric(txtdistance.text) and isnumeric(txtangle.text) then diameter = txtdiameter.text distance = txtdistance.text angle = txtangle.text hide else msgbox "you must enter numeric values for all fields" end if end sub 10. select everything in your main code that you posted to me originally and delete it. then copy this the following code in there. 'modified to make these early bound so you can see the intellisense drop downs while typing in code********** dim swapp as sldworks.sldworks dim part as sldworks.modeldoc2 dim selmgr as sldworks.selectionmgr 'added ************************ dim revdiameter as double dim revdistance as double dim revangle as double '************************* dim boolstatus as boolean sub main() set swapp = application.sldworks set part = swapp.activedoc set selmgr = part.selectionmanager 'added ************************************************** ' create an instance of the user form dim newform as new frmrevolve newform.caption = "enter values in millimeters" newform.show revdiameter = newform.diameter / 1000 'converting from millimeters to meters revdistance = newform.distance / 1000 ''converting from millimeters to meters revangle = newform.angle / 57.29577951 'converting from radians to degrees 'destroy the instance of the user form set newform = nothing '********************************************************* part.clearselection2 true dim skcircle as object set skcircle = part.sketchmanager.createcircle(-0.09628480219746, 0, 0, -0.07566040614345, -0.007297863219111, 0) part.clearselection2 true boolstatus = part.extension.selectbyid2("arc1", "sketchsegment", -0.08898693897835, 0.01998979925235, 0, false, 0, nothing, 0) dim annotation as object set annotation = part.adddimension2(-0.0985059, 0.0402969, 0) part.clearselection2 true part.parameter("<A href="mailto:d1@sketch1").systemvalue">d1@sketch1").systemvalue = revdiameter ' changed this to my var part.viewzoomtofit2 part.clearselection2 true dim skline as object set skline = part.sketchmanager.createcenterline(0.08742431791818, -0.04545380291521, 0, 0.08742431791818, 0.09217021146695, 0) part.clearselection2 true boolstatus = part.extension.selectbyid2("line1", "sketchsegment", 0.0882660550092, 0.001262605636534, 5.000000000001e-05, false, 0, nothing, 0) boolstatus = part.extension.selectbyid2("point2", "sketchpoint", -0.09628480219746, 0, 0, true, 0, nothing, 0) set annotation = part.adddimension2(-0.00179981, -0.119106, 0) part.clearselection2 true part.parameter("<A href="mailto:d2@sketch1").systemvalue">d2@sketch1").systemvalue = revdistance ' changed this to my var part.viewzoomtofit2 boolstatus = part.extension.selectbyid2("line1", "sketchsegment", 0.08742431791818, -0.04545380291521, 0, true, 0, nothing, 0) part.shownamedview2 "*trimetric", 8 part.clearselection2 true boolstatus = part.extension.selectbyid2("d2@sketch1@part3.sldprt", "dimension", 0, 0, 0, false, 0, nothing, 0) boolstatus = part.extension.selectbyid2("line1", "sketchsegment", 0.08742431791818, -0.04545380291521, 0, true, 4, nothing, 0) boolstatus = part.extension.selectbyid2("sketch1", "sketch", 0, 0, 0, true, 0, nothing, 0) part.featuremanager.featurerevolve revangle, false, 0, 0, 0, 1, 1, 1 ' changed this to my var part.selectionmanager.enablecontourselection = 0 part.viewzoomtofit2 end sub 11. run it! i ran it here and it worked but i am using sw2008 sp4 and i am not sure what your using. let me know! thanks. solidworks professional 2009 sp0 dual core amd opteron 2.21ghz cpu 4 gb ram nvidia quadrofx 4500 w/ 512mb ram edited: 10/17/2008 at 11:27 am by jake johnson yeah i dont know if modeldoc2 existed back then change this area of code: dim swapp as sldworks.sldworks dim part as sldworks.modeldoc2 <----- it might not be handling this right dim selmgr as sldworks.selectionmgr to dim swapp as object dim part as object dim selmgr as object or you could change modeldoc2 to modeldoc because i think that did exist back then. solidworks professional 2009 sp0 dual core amd opteron 2.21ghz cpu 4 gb ram nvidia quadrofx 4500 w/ 512mb ram edited: 10/20/2008 at 07:53 am by jake johnson i need a little more detail than that to understand you properly when you say the "sketch is asking again for the diameter and distance." is something coming up asking you for values? (after your user form already asked you for them) then you had said that the the angle went in automatically. do you mean that it worked correctly? or it put in some other angle that is not correct. thanks! email me at jaj@kenmode.com if you want and we can work this out. it might be a better way to reach me since i don't check this a lot solidworks professional 2009 sp0 dual core amd opteron 2.21ghz cpu 4 gb ram nvidia quadrofx 4500 w/ 512mb ram right after this code: 'added ************************************************** ' create an instance of the user form dim newform as new frmrevolve newform.caption = "enter values in millimeters" newform.show revdiameter = newform.diameter / 1000 'converting from millimeters to meters revdistance = newform.distance / 1000 ''converting from millimeters to meters revangle = newform.angle / 57.29577951 'converting from radians to degrees 'destroy the instance of the user form set newform = nothing add the following code: msgbox "dia = " & revdiameter & vbcrlf & "dist = " & revdistance & vbcrlf & "angle = " & revangle this will confirm that those values are making it from your userform to the main code. (if you see values in the messagebox) if those values do not show up here - the problem is in the userform somewhere. double check your spelling and that all the variable definitions are correct and where they should be. if you can't find out what is wrong, please copy and paste your userform and main code here so i can look at it. thanks solidworks professional 2009 sp0 dual core amd opteron 2.21ghz cpu 4 gb ram nvidia quadrofx 4500 w/ 512mb ram this might be a setting under the options of solidworks. look in options under: options -> general -> input dimension uncheck this checkbox if it is checked and try the code. if this works we would need to find a way to surpress the input dimension function inside the code. it might be a property of the add dimension method. *edit* yes that is exactly the issue - i will look into what it takes to surpress that. alternatively you can just turn that option off if you want and it will work like it does on my system. *edit2* the code you can use is: put this near the top of your main code - swapp.setuserpreferencetoggle swinputdimvaloncreate, false then at the bottom of your main code add this line - swapp.setuserpreferencetoggle swinputdimvaloncreate, true it will turn this option off, then run your code and afterwards it will turn this back on agian. solidworks professional 2009 sp0 dual core amd opteron 2.21ghz cpu 4 gb ram nvidia quadrofx 4500 w/ 512mb ram edited: 10/22/2008 at 08:29 am by jake johnson no problem i'm glad it worked out for you! solidworks professional 2009 sp0 dual core amd opteron 2.21ghz cpu 4 gb ram nvidia quadrofx 4500 w/ 512mb ram this might be a silly question, but do you have any expirience with design tables in solidworks? solidworks uses excel in design tables to do exactly what you are talking about ( i think) if you want some help setting one up, let me know solidworks professional 2009 sp0 dual core amd opteron 2.21ghz cpu 4 gb ram nvidia quadrofx 4500 w/ 512mb ram |
所有的时间均为北京时间。 现在的时间是 09:50 AM. |