Jump to content

Welcome to eMastercam

Register now to participate in the forums, access the download area, buy Mastercam training materials, post processors and more. This message will be removed once you have signed in.

Use your display name or email address to sign in:

Iterate thru entities in a chain Nethook ver. 2022


Recommended Posts

Hello all, I have a need to cycle thru all the entities in a selected chain and change the color and level of each. I have a valid chain and now need to iterate thru each entity in said chain.

Here is what I have

Dim chain As Mastercam.Database.Chain()
        Dim Count As Integer

        chain = GetChain("Select the CENTER containment chain.")

        Count = chain(0).CurvesCount

If (chain.Any()) Then '// set color, level and line width of each 
           For Each Entity As Mastercam.Database.Geometry In chain()
                Entity.Color = 31 '// Light Green
                Entity.Level = 20
                Entity.LineWidth = 4
            Next
        End If

 Private Function GetChain(Prompt As String) As Mastercam.Database.Chain()
        '// select the chain for the organize routine
        Call ClearGeometry()
        GetChain = ChainManager.GetMultipleChains(Prompt, False)

    End Function

 

I have a valid chain as the .CurvesCount returns the correct number of entities, but I get an error on the for each line stating that "Value type 'chain' cannot be converted to type 'geometry'

 

Thanks in advance

Link to comment
Share on other sites

That's not going to work, what you are looping through is an array of chains, not an array of entities.

 

Iirc there is a function in the chainmanager to get the entities from each chain and return them as an array.

Link to comment
Share on other sites

In c# I think this is what the code might look like

var chain = ChainManager.GetMultipleChains("Select the CENTER containment chain.", false);

if(chain == null){return;}
if(chain.Length < 1){return;}

var Count = chain[0].CurvesCount;

if (chain.Length > 0)// '// set color, level and line width of each 
{
	foreach(var chn in chain)
	{
		var entities = Mastercam.Database.ChainManager.GetGeometryInChain(chn);
		if(entities == null){return;}
		foreach(var entity in entities)
		{

     	 entity.Color = 31;//'// Light Green
	 	 entity.Level = 20;
    	 entity.LineWidth = 4;
		 entity.Commit();//Commit the changes to the database
		}
	}
 }

In Vb.net

Private Sub SurroundingSub()
    Dim chain = ChainManager.GetMultipleChains("Select the CENTER containment chain.", False)

    If chain Is Nothing Then
        Return
    End If

    If chain.Length < 1 Then
        Return
    End If

    Dim Count = chain(0).CurvesCount

    If chain.Length > 0 Then

        For Each chn In chain
            Dim entities = Mastercam.Database.ChainManager.GetGeometryInChain(chn)

            If entities Is Nothing Then
                Return
            End If

            For Each entity In entities
                entity.Color = 31
                entity.Level = 20
                entity.LineWidth = 4
                entity.Commit()
            Next
        Next
    End If
End Sub

 

Link to comment
Share on other sites

Thank you. That was what I was looking for. I got this to work:

For Each Entity As Mastercam.Database.CurveGeometry In Mastercam.Database.ChainManager.GetGeometryInChain(chain(0))
                Entity.Color = 31 '// Light Green
                Entity.Level = 20
                Entity.LineWidth = 4
                Entity.Commit()
            Next

But that brought up anpther question as to how to determine if there was actually a valid chain. Your solution answerd that even before I asked.

Thank you again very much appreciated. 

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.

Join us!

eMastercam - your online source for all things Mastercam.

Together, we are the strongest Mastercam community on the web with over 56,000 members, and our online store offers a wide selection of training materials for all applications and skill levels.

Follow us

×
×
  • Create New...