Can anyone tell me what code i have to use. I can see the length in the Feature Manager, but I have no idea how to obtain the length via vba or vb.net.
FYI, a macro that does this is available to paid members of CADSharp.com. It's in our Macro Library and is called "Get belt length and add to belt component’s custom properties". It uses recursion to find the correct sketch and component.
I am not aware of a way to directly get the length value that is in parentheses next to the belt feature. I had to enter the sketch and sum the lengths of the sketch segments.
SolidWorks API Training and Services
After a bit of investigation, I think this answers your question:
SOLIDWORKS Foums API/Macros: How to get the length of a belt feature? - YouTube
(Ignore my other answer)
Do I understand this correct, there is no way to get the length of a belt by just reading a value somewhere?
How does Solidworks get the length of 6137.27mm in this example and keep it after saving? I mean, the software does not calculate it everytime the assembly is loaded, does it?
Oh the software does so many things when it loads an assembly, don't be surprised. It looks like you have 2016-or 2017-from the looks of the belt feature icon, so I think you can calculate the belt length.
I have 2017. I have found a way to get the length, but now I have the problem that I need to access the Part Document beneath the belt feature. Traversing through the assembly is no problem, but how can I determine that this part is the part of the belt feature? Haven't found a solution yet. Any idea?
What you're looking for then is to access the selection that defines the chain feature through this method:
AccessSelections Method (IChainPatternFeatureData)
Gains access to the selections that define this chain pattern feature.
Function AccessSelections( _ ByVal TopDoc As System.Object, _ ByVal Component As System.Object _ ) As System.Boolean
Dim instance As IChainPatternFeatureData Dim TopDoc As System.Object Dim Component As System.Object Dim value As System.Boolean value = instance.AccessSelections(TopDoc, Component)
System.bool AccessSelections( System.object TopDoc, System.object Component )
System.bool AccessSelections( & System.Object^ TopDoc, & System.Object^ Component )
NOTE: See Differences Between Unmanaged C++ and C++/CLI Code.
True if the selections are successfully accessed, false if not
Modify Chain Pattern Feature (C#) Modify Chain Pattern Feature (VB.NET) Modify Chain Pattern Feature (VBA) Create and Modify Distance Chain Pattern Feature (C#) Create and Modify Distance Chain Pattern Feature (VB.NET) Create and Modify Distance Chain Pattern Feature (VBA)
This method puts the model into a rollback state to allow access to the selections that define this feature. You must use either of the following methods to restore the rollback state:
I think we are talking about different things.
I traverse through the features in my assembly and I find : Feature.GetTypeName = "Belt"
But I do that by Feature Traversal.
From here on I do not know how to obtain the part document below the belt feature, because the Feature does not know about the Component and , as far as I can tell, the Component does not know about the Feature.
The thing is that I want to write the length into the Custom Properties if the Part.
Is it possible send me the assembly - firstname.lastname@example.org?
Thank you for your help so far.
But I have still some problems.
Set swCompFeature = swFeature.GetNextSubFeature
my swCompFeature is Nothing.
Do you have any idea why?
I have tested your code with the Test assembly I've sent you.
Hm. Is there a belt feature that only contains a,sketch in your assembly. Add a if statement that checks if the subfeature is nothing and see what happens.
At first I thought it has something to do with a virtual part, so I saved the belt part.
After that I tried this code:
with this result:
The part document of the belt is above the matefolder. And "Skizze2" is missing.
I am currently checking the api Release Notes. Haven't found anything that would explain this behavior. In your video everything works fine. But you used the 2016 version of Solidworks, right?
( I'm running the german version of Solidworks. "Verknüpfungen" is the Matefolder, "Riemen1" is Belt1, "Ursprung" is Origin)
I have extended your code with this:
the result is:
No more subfeatures.
I'm using sw16. GetFeatures returns features in no particular order.
If TopLevelOnly is false, then this method gets all of the feature and child features in this document. It does not get features in components.
The features that are returned by this method can be in any order. You should not rely on the order to indicate anything about children or parents. If hierarchy and order information is needed, then use IPartDoc::FirstFeature or IPartDoc::IFirstFeature or IModelDoc2::FirstFeature or IModelDoc2::FirstFeature, IFeature::GetNextFeature or IFeatureIGetNextFeature, IFeature::GetFirstSubFeature or IFeature::IGetFirstSubFeature, and IFeature::GetNextSubFeature or IFeature::IGetNextSubFeature to retrieve your information.
I have installed Solidworks 2016 SP05 and tested your macro. The result is the same as in Solidworks 2017. Why did it work on your Solidworks? I mean the video on youtube is best proof. But whatever i do, i can not reproduce your result.
Or is there another way to get the belt length as a custom property into the belt part?
I think this is to do with the features order. If you find me an example that doesn't produce the desired results like yours, I'll give it a go this weekend!
What do you mean with "if you find me an example that doesn't produce the desired results like yours" ?
Your example in the video works. The exact same macro code doesn't work on my computer.
I also asked the api support at solidworks with the following answer:
We have following observations on your workflow:
1. The part created in the Belt feature is actually a Component and not a Sub Feature of Belt Feature, so in SOLIDWORKS 2017,GetNextSubFeature method returns nothing.
2. API help documentation of GetSpecificFeature2 Method (IFeature) explains about which objects this method returns, This method doesn't return the IComponent object. (Refer remark section http://help.solidworks.com/2017/english/api/sldworksapi/solidworks.interop.sldworks~solidworks.interop.sldworks.ifeature~getspecificfeature2.html )
3. If you want the Component information ( which is inside the Belt feature) then you need to traverse the Assembly components.
I'm a bit lost here, because I traversed the assembly but I can not identify the belt part, because I have no reference to the part inside the belt feature.
Look at my first answer!
forgive my typo. I was trying to ask if you can send me an example assembly like the one you are dealing with so I further test it on my end. It looks like the example I've used is not like the one you have.
Retrieving data ...